xref: /freebsd/sys/fs/nfsclient/nfs_clcomsubs.c (revision 6fa843f6e647a1a1e0e42af1e7abc9e903699f31)
19ec7b004SRick Macklem /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
49ec7b004SRick Macklem  * Copyright (c) 1989, 1993
59ec7b004SRick Macklem  *	The Regents of the University of California.  All rights reserved.
69ec7b004SRick Macklem  *
79ec7b004SRick Macklem  * This code is derived from software contributed to Berkeley by
89ec7b004SRick Macklem  * Rick Macklem at The University of Guelph.
99ec7b004SRick Macklem  *
109ec7b004SRick Macklem  * Redistribution and use in source and binary forms, with or without
119ec7b004SRick Macklem  * modification, are permitted provided that the following conditions
129ec7b004SRick Macklem  * are met:
139ec7b004SRick Macklem  * 1. Redistributions of source code must retain the above copyright
149ec7b004SRick Macklem  *    notice, this list of conditions and the following disclaimer.
159ec7b004SRick Macklem  * 2. Redistributions in binary form must reproduce the above copyright
169ec7b004SRick Macklem  *    notice, this list of conditions and the following disclaimer in the
179ec7b004SRick Macklem  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199ec7b004SRick Macklem  *    may be used to endorse or promote products derived from this software
209ec7b004SRick Macklem  *    without specific prior written permission.
219ec7b004SRick Macklem  *
229ec7b004SRick Macklem  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239ec7b004SRick Macklem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249ec7b004SRick Macklem  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259ec7b004SRick Macklem  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269ec7b004SRick Macklem  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279ec7b004SRick Macklem  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289ec7b004SRick Macklem  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299ec7b004SRick Macklem  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309ec7b004SRick Macklem  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319ec7b004SRick Macklem  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329ec7b004SRick Macklem  * SUCH DAMAGE.
339ec7b004SRick Macklem  *
349ec7b004SRick Macklem  */
359ec7b004SRick Macklem 
369ec7b004SRick Macklem #include <sys/cdefs.h>
379ec7b004SRick Macklem /*
389ec7b004SRick Macklem  * These functions support the macros and help fiddle mbuf chains for
399ec7b004SRick Macklem  * the nfs op functions. They do things like create the rpc header and
409ec7b004SRick Macklem  * copy data between mbuf chains and uio lists.
419ec7b004SRick Macklem  */
429ec7b004SRick Macklem #include <fs/nfs/nfsport.h>
439ec7b004SRick Macklem 
441b819cf2SRick Macklem extern struct nfsstatsv1 nfsstatsv1;
459ec7b004SRick Macklem extern int ncl_mbuf_mlen;
46ba8cc6d7SMateusz Guzik extern __enum_uint8(vtype) newnv2tov_type[8];
47ba8cc6d7SMateusz Guzik extern __enum_uint8(vtype) nv34tov_type[8];
489ec7b004SRick Macklem NFSCLSTATEMUTEX;
499ec7b004SRick Macklem 
509ec7b004SRick Macklem /*
519ec7b004SRick Macklem  * copies a uio scatter/gather list to an mbuf chain.
52c1ad8a39SGordon Bergling  * NOTE: can only handle iovcnt == 1
539ec7b004SRick Macklem  */
54*6fa843f6SMark Johnston int
nfsm_uiombuf(struct nfsrv_descript * nd,struct uio * uiop,int siz)559ec7b004SRick Macklem nfsm_uiombuf(struct nfsrv_descript *nd, struct uio *uiop, int siz)
569ec7b004SRick Macklem {
579ec7b004SRick Macklem 	char *uiocp;
589ec7b004SRick Macklem 	struct mbuf *mp, *mp2;
59*6fa843f6SMark Johnston 	int error, xfer, left, mlen;
609ec7b004SRick Macklem 	int uiosiz, clflg, rem;
613eaf0376SRick Macklem 	char *mcp, *tcp;
629ec7b004SRick Macklem 
63b38f7723SKonstantin Belousov 	KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
649ec7b004SRick Macklem 
659ec7b004SRick Macklem 	if (siz > ncl_mbuf_mlen)	/* or should it >= MCLBYTES ?? */
669ec7b004SRick Macklem 		clflg = 1;
679ec7b004SRick Macklem 	else
689ec7b004SRick Macklem 		clflg = 0;
699ec7b004SRick Macklem 	rem = NFSM_RNDUP(siz) - siz;
709ec7b004SRick Macklem 	mp = mp2 = nd->nd_mb;
713eaf0376SRick Macklem 	mcp = nd->nd_bpos;
729ec7b004SRick Macklem 	while (siz > 0) {
733eaf0376SRick Macklem 		KASSERT((nd->nd_flag & ND_EXTPG) != 0 || mcp ==
743eaf0376SRick Macklem 		    mtod(mp, char *) + mp->m_len, ("nfsm_uiombuf: mcp wrong"));
759ec7b004SRick Macklem 		left = uiop->uio_iov->iov_len;
769ec7b004SRick Macklem 		uiocp = uiop->uio_iov->iov_base;
779ec7b004SRick Macklem 		if (left > siz)
789ec7b004SRick Macklem 			left = siz;
799ec7b004SRick Macklem 		uiosiz = left;
809ec7b004SRick Macklem 		while (left > 0) {
813eaf0376SRick Macklem 			if ((nd->nd_flag & ND_EXTPG) != 0)
823eaf0376SRick Macklem 				mlen = nd->nd_bextpgsiz;
833eaf0376SRick Macklem 			else
849ec7b004SRick Macklem 				mlen = M_TRAILINGSPACE(mp);
859ec7b004SRick Macklem 			if (mlen == 0) {
863eaf0376SRick Macklem 				if ((nd->nd_flag & ND_EXTPG) != 0) {
873eaf0376SRick Macklem 					mp = nfsm_add_ext_pgs(mp,
883eaf0376SRick Macklem 					    nd->nd_maxextsiz, &nd->nd_bextpg);
893eaf0376SRick Macklem 					mcp = (char *)(void *)PHYS_TO_DMAP(
903eaf0376SRick Macklem 					  mp->m_epg_pa[nd->nd_bextpg]);
9190cf38f2SRick Macklem 					nd->nd_bextpgsiz = mlen = PAGE_SIZE;
923eaf0376SRick Macklem 				} else {
939ec7b004SRick Macklem 					if (clflg)
94eb1b1807SGleb Smirnoff 						NFSMCLGET(mp, M_WAITOK);
959ec7b004SRick Macklem 					else
969ec7b004SRick Macklem 						NFSMGET(mp);
973133bbf7SRick Macklem 					mp->m_len = 0;
983eaf0376SRick Macklem 					mlen = M_TRAILINGSPACE(mp);
993eaf0376SRick Macklem 					mcp = mtod(mp, char *);
1003133bbf7SRick Macklem 					mp2->m_next = mp;
1019ec7b004SRick Macklem 					mp2 = mp;
1023eaf0376SRick Macklem 				}
1039ec7b004SRick Macklem 			}
1049ec7b004SRick Macklem 			xfer = (left > mlen) ? mlen : left;
1059ec7b004SRick Macklem 			if (uiop->uio_segflg == UIO_SYSSPACE)
1063eaf0376SRick Macklem 				NFSBCOPY(uiocp, mcp, xfer);
107*6fa843f6SMark Johnston 			else {
108*6fa843f6SMark Johnston 				error = copyin(uiocp, mcp, xfer);
109*6fa843f6SMark Johnston 				if (error != 0)
110*6fa843f6SMark Johnston 					return (error);
111*6fa843f6SMark Johnston 			}
1123133bbf7SRick Macklem 			mp->m_len += xfer;
1139ec7b004SRick Macklem 			left -= xfer;
1149ec7b004SRick Macklem 			uiocp += xfer;
1153eaf0376SRick Macklem 			mcp += xfer;
1163eaf0376SRick Macklem 			if ((nd->nd_flag & ND_EXTPG) != 0) {
1173eaf0376SRick Macklem 				nd->nd_bextpgsiz -= xfer;
1183eaf0376SRick Macklem 				mp->m_epg_last_len += xfer;
1193eaf0376SRick Macklem 			}
1209ec7b004SRick Macklem 			uiop->uio_offset += xfer;
1219ec7b004SRick Macklem 			uiop->uio_resid -= xfer;
1229ec7b004SRick Macklem 		}
1239ec7b004SRick Macklem 		tcp = (char *)uiop->uio_iov->iov_base;
1249ec7b004SRick Macklem 		tcp += uiosiz;
1259ec7b004SRick Macklem 		uiop->uio_iov->iov_base = (void *)tcp;
1269ec7b004SRick Macklem 		uiop->uio_iov->iov_len -= uiosiz;
1279ec7b004SRick Macklem 		siz -= uiosiz;
1289ec7b004SRick Macklem 	}
1299ec7b004SRick Macklem 	if (rem > 0) {
1303eaf0376SRick Macklem 		if ((nd->nd_flag & ND_EXTPG) == 0 && rem >
1313eaf0376SRick Macklem 		    M_TRAILINGSPACE(mp)) {
1329ec7b004SRick Macklem 			NFSMGET(mp);
1333133bbf7SRick Macklem 			mp->m_len = 0;
1343133bbf7SRick Macklem 			mp2->m_next = mp;
1353eaf0376SRick Macklem 			mcp = mtod(mp, char *);
1363eaf0376SRick Macklem 		} else if ((nd->nd_flag & ND_EXTPG) != 0 && rem >
1373eaf0376SRick Macklem 		    nd->nd_bextpgsiz) {
1383eaf0376SRick Macklem 			mp = nfsm_add_ext_pgs(mp, nd->nd_maxextsiz,
1393eaf0376SRick Macklem 			    &nd->nd_bextpg);
1403eaf0376SRick Macklem 			mcp = (char *)(void *)
1413eaf0376SRick Macklem 			    PHYS_TO_DMAP(mp->m_epg_pa[nd->nd_bextpg]);
1423eaf0376SRick Macklem 			nd->nd_bextpgsiz = PAGE_SIZE;
1439ec7b004SRick Macklem 		}
1449ec7b004SRick Macklem 		for (left = 0; left < rem; left++)
1453eaf0376SRick Macklem 			*mcp++ = '\0';
1463133bbf7SRick Macklem 		mp->m_len += rem;
1473eaf0376SRick Macklem 		if ((nd->nd_flag & ND_EXTPG) != 0) {
1483eaf0376SRick Macklem 			nd->nd_bextpgsiz -= rem;
1493eaf0376SRick Macklem 			mp->m_epg_last_len += rem;
1503eaf0376SRick Macklem 		}
1516722f6e5SRick Macklem 	}
1523eaf0376SRick Macklem 	nd->nd_bpos = mcp;
1539ec7b004SRick Macklem 	nd->nd_mb = mp;
154*6fa843f6SMark Johnston 	return (0);
1559ec7b004SRick Macklem }
1562742a210SRick Macklem 
1572742a210SRick Macklem /*
1582742a210SRick Macklem  * copies a uio scatter/gather list to an mbuf chain.
1592742a210SRick Macklem  * This version returns the mbuf list and does not use "nd".
160c1ad8a39SGordon Bergling  * NOTE: can only handle iovcnt == 1
1612742a210SRick Macklem  */
1622742a210SRick Macklem struct mbuf *
nfsm_uiombuflist(struct uio * uiop,int siz,u_int maxext)163cfaafa79SRick Macklem nfsm_uiombuflist(struct uio *uiop, int siz, u_int maxext)
1642742a210SRick Macklem {
1652742a210SRick Macklem 	char *uiocp;
1662742a210SRick Macklem 	struct mbuf *mp, *mp2, *firstmp;
167*6fa843f6SMark Johnston 	int error, extpg, extpgsiz = 0, i, left, mlen, rem, xfer;
168b7faa59dSMatt Macy 	int uiosiz, clflg;
1699516bcdfSRick Macklem 	char *mcp, *tcp;
1702742a210SRick Macklem 
1712742a210SRick Macklem 	KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
1722742a210SRick Macklem 
173cfaafa79SRick Macklem 	if (maxext > 0) {
174cfaafa79SRick Macklem 		mp = mb_alloc_ext_plus_pages(PAGE_SIZE, M_WAITOK);
175cfaafa79SRick Macklem 		mcp = (char *)(void *)PHYS_TO_DMAP(mp->m_epg_pa[0]);
176cfaafa79SRick Macklem 		extpg = 0;
177cfaafa79SRick Macklem 		extpgsiz = PAGE_SIZE;
178cfaafa79SRick Macklem 	} else {
1792742a210SRick Macklem 		if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */
1802742a210SRick Macklem 			clflg = 1;
1812742a210SRick Macklem 		else
1822742a210SRick Macklem 			clflg = 0;
1832742a210SRick Macklem 		if (clflg != 0)
1842742a210SRick Macklem 			NFSMCLGET(mp, M_WAITOK);
1852742a210SRick Macklem 		else
1862742a210SRick Macklem 			NFSMGET(mp);
1879516bcdfSRick Macklem 		mcp = mtod(mp, char *);
188cfaafa79SRick Macklem 	}
189cfaafa79SRick Macklem 	mp->m_len = 0;
1902742a210SRick Macklem 	firstmp = mp2 = mp;
1919516bcdfSRick Macklem 	rem = NFSM_RNDUP(siz) - siz;
1922742a210SRick Macklem 	while (siz > 0) {
1932742a210SRick Macklem 		left = uiop->uio_iov->iov_len;
1942742a210SRick Macklem 		uiocp = uiop->uio_iov->iov_base;
1952742a210SRick Macklem 		if (left > siz)
1962742a210SRick Macklem 			left = siz;
1972742a210SRick Macklem 		uiosiz = left;
1982742a210SRick Macklem 		while (left > 0) {
199cfaafa79SRick Macklem 			if (maxext > 0)
200cfaafa79SRick Macklem 				mlen = extpgsiz;
201cfaafa79SRick Macklem 			else
2022742a210SRick Macklem 				mlen = M_TRAILINGSPACE(mp);
2032742a210SRick Macklem 			if (mlen == 0) {
204cfaafa79SRick Macklem 				if (maxext > 0) {
205cfaafa79SRick Macklem 					mp = nfsm_add_ext_pgs(mp, maxext,
206cfaafa79SRick Macklem 					    &extpg);
207cfaafa79SRick Macklem 					mlen = extpgsiz = PAGE_SIZE;
208cfaafa79SRick Macklem 					mcp = (char *)(void *)PHYS_TO_DMAP(
209cfaafa79SRick Macklem 					    mp->m_epg_pa[extpg]);
210cfaafa79SRick Macklem 				} else {
2112742a210SRick Macklem 					if (clflg)
2122742a210SRick Macklem 						NFSMCLGET(mp, M_WAITOK);
2132742a210SRick Macklem 					else
2142742a210SRick Macklem 						NFSMGET(mp);
2159516bcdfSRick Macklem 					mcp = mtod(mp, char *);
216cfaafa79SRick Macklem 					mlen = M_TRAILINGSPACE(mp);
217cfaafa79SRick Macklem 					mp->m_len = 0;
2183133bbf7SRick Macklem 					mp2->m_next = mp;
2192742a210SRick Macklem 					mp2 = mp;
220cfaafa79SRick Macklem 				}
2212742a210SRick Macklem 			}
2222742a210SRick Macklem 			xfer = (left > mlen) ? mlen : left;
2232742a210SRick Macklem 			if (uiop->uio_segflg == UIO_SYSSPACE)
2249516bcdfSRick Macklem 				NFSBCOPY(uiocp, mcp, xfer);
225*6fa843f6SMark Johnston 			else {
226*6fa843f6SMark Johnston 				error = copyin(uiocp, mcp, xfer);
227*6fa843f6SMark Johnston 				if (error != 0) {
228*6fa843f6SMark Johnston 					m_freem(firstmp);
229*6fa843f6SMark Johnston 					return (NULL);
230*6fa843f6SMark Johnston 				}
231*6fa843f6SMark Johnston 			}
2323133bbf7SRick Macklem 			mp->m_len += xfer;
2339516bcdfSRick Macklem 			mcp += xfer;
234cfaafa79SRick Macklem 			if (maxext > 0) {
235cfaafa79SRick Macklem 				extpgsiz -= xfer;
236cfaafa79SRick Macklem 				mp->m_epg_last_len += xfer;
237cfaafa79SRick Macklem 			}
2382742a210SRick Macklem 			left -= xfer;
2392742a210SRick Macklem 			uiocp += xfer;
2402742a210SRick Macklem 			uiop->uio_offset += xfer;
2412742a210SRick Macklem 			uiop->uio_resid -= xfer;
2422742a210SRick Macklem 		}
2432742a210SRick Macklem 		tcp = (char *)uiop->uio_iov->iov_base;
2442742a210SRick Macklem 		tcp += uiosiz;
2452742a210SRick Macklem 		uiop->uio_iov->iov_base = (void *)tcp;
2462742a210SRick Macklem 		uiop->uio_iov->iov_len -= uiosiz;
2472742a210SRick Macklem 		siz -= uiosiz;
2482742a210SRick Macklem 	}
2499516bcdfSRick Macklem 	if (rem > 0) {
250cfaafa79SRick Macklem 		KASSERT((mp->m_flags & M_EXTPG) != 0 ||
251cfaafa79SRick Macklem 		    rem <= M_TRAILINGSPACE(mp),
2529516bcdfSRick Macklem 		    ("nfsm_uiombuflist: no space for padding"));
2539516bcdfSRick Macklem 		for (i = 0; i < rem; i++)
2549516bcdfSRick Macklem 			*mcp++ = '\0';
2559516bcdfSRick Macklem 		mp->m_len += rem;
256cfaafa79SRick Macklem 		if (maxext > 0)
257cfaafa79SRick Macklem 			mp->m_epg_last_len += rem;
2589516bcdfSRick Macklem 	}
2592742a210SRick Macklem 	return (firstmp);
2602742a210SRick Macklem }
2619ec7b004SRick Macklem 
2629ec7b004SRick Macklem /*
2639ec7b004SRick Macklem  * Load vnode attributes from the xdr file attributes.
2649ec7b004SRick Macklem  * Returns EBADRPC if they can't be parsed, 0 otherwise.
2659ec7b004SRick Macklem  */
266b9cc3262SRyan Moeller int
nfsm_loadattr(struct nfsrv_descript * nd,struct nfsvattr * nap)2679ec7b004SRick Macklem nfsm_loadattr(struct nfsrv_descript *nd, struct nfsvattr *nap)
2689ec7b004SRick Macklem {
2699ec7b004SRick Macklem 	struct nfs_fattr *fp;
2709ec7b004SRick Macklem 	int error = 0;
2719ec7b004SRick Macklem 
2729ec7b004SRick Macklem 	if (nd->nd_flag & ND_NFSV4) {
2739ec7b004SRick Macklem 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL,
2749ec7b004SRick Macklem 		    NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
2759ec7b004SRick Macklem 	} else if (nd->nd_flag & ND_NFSV3) {
2769ec7b004SRick Macklem 		NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V3FATTR);
2779ec7b004SRick Macklem 		nap->na_type = nfsv34tov_type(fp->fa_type);
2789ec7b004SRick Macklem 		nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
279ab35e1c7SBruce Evans 		nap->na_rdev = NFSMAKEDEV(
280ab35e1c7SBruce Evans 		    fxdr_unsigned(int, fp->fa3_rdev.specdata1),
281ab35e1c7SBruce Evans 		    fxdr_unsigned(int, fp->fa3_rdev.specdata2));
282a0a073b1SJohn Baldwin 		nap->na_nlink = fxdr_unsigned(uint32_t, fp->fa_nlink);
2839ec7b004SRick Macklem 		nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
2849ec7b004SRick Macklem 		nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
2859ec7b004SRick Macklem 		nap->na_size = fxdr_hyper(&fp->fa3_size);
2869ec7b004SRick Macklem 		nap->na_blocksize = NFS_FABLKSIZE;
2879ec7b004SRick Macklem 		nap->na_bytes = fxdr_hyper(&fp->fa3_used);
288418802a9SZack Kirsch 		nap->na_fileid = fxdr_hyper(&fp->fa3_fileid);
2899ec7b004SRick Macklem 		fxdr_nfsv3time(&fp->fa3_atime, &nap->na_atime);
2909ec7b004SRick Macklem 		fxdr_nfsv3time(&fp->fa3_ctime, &nap->na_ctime);
2919ec7b004SRick Macklem 		fxdr_nfsv3time(&fp->fa3_mtime, &nap->na_mtime);
292dd02d9d6SRick Macklem 		nap->na_btime.tv_sec = -1;
293dd02d9d6SRick Macklem 		nap->na_btime.tv_nsec = 0;
2949ec7b004SRick Macklem 		nap->na_flags = 0;
2958bde6d15SMark Johnston 		nap->na_gen = 0;
2969ec7b004SRick Macklem 		nap->na_filerev = 0;
2979ec7b004SRick Macklem 	} else {
2989ec7b004SRick Macklem 		NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V2FATTR);
2999ec7b004SRick Macklem 		nap->na_type = nfsv2tov_type(fp->fa_type);
3009ec7b004SRick Macklem 		nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
3019ec7b004SRick Macklem 		if (nap->na_type == VNON || nap->na_type == VREG)
3029ec7b004SRick Macklem 			nap->na_type = IFTOVT(nap->na_mode);
3039ec7b004SRick Macklem 		nap->na_rdev = fxdr_unsigned(dev_t, fp->fa2_rdev);
3049ec7b004SRick Macklem 
3059ec7b004SRick Macklem 		/*
3069ec7b004SRick Macklem 		 * Really ugly NFSv2 kludge.
3079ec7b004SRick Macklem 		 */
3089ec7b004SRick Macklem 		if (nap->na_type == VCHR && nap->na_rdev == ((dev_t)-1))
3099ec7b004SRick Macklem 			nap->na_type = VFIFO;
3109ec7b004SRick Macklem 		nap->na_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
3119ec7b004SRick Macklem 		nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
3129ec7b004SRick Macklem 		nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
3139ec7b004SRick Macklem 		nap->na_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
3149ec7b004SRick Macklem 		nap->na_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
3159ec7b004SRick Macklem 		nap->na_bytes =
3169ec7b004SRick Macklem 		    (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) *
3179ec7b004SRick Macklem 		    NFS_FABLKSIZE;
318418802a9SZack Kirsch 		nap->na_fileid = fxdr_unsigned(uint64_t, fp->fa2_fileid);
3199ec7b004SRick Macklem 		fxdr_nfsv2time(&fp->fa2_atime, &nap->na_atime);
3209ec7b004SRick Macklem 		fxdr_nfsv2time(&fp->fa2_mtime, &nap->na_mtime);
3219ec7b004SRick Macklem 		nap->na_flags = 0;
3229ec7b004SRick Macklem 		nap->na_ctime.tv_sec = fxdr_unsigned(u_int32_t,
3239ec7b004SRick Macklem 		    fp->fa2_ctime.nfsv2_sec);
3249ec7b004SRick Macklem 		nap->na_ctime.tv_nsec = 0;
325dd02d9d6SRick Macklem 		nap->na_btime.tv_sec = -1;
326dd02d9d6SRick Macklem 		nap->na_btime.tv_nsec = 0;
3279ec7b004SRick Macklem 		nap->na_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
3289ec7b004SRick Macklem 		nap->na_filerev = 0;
3299ec7b004SRick Macklem 	}
3309ec7b004SRick Macklem nfsmout:
3319ec7b004SRick Macklem 	return (error);
3329ec7b004SRick Macklem }
3339ec7b004SRick Macklem 
3349ec7b004SRick Macklem /*
3359ec7b004SRick Macklem  * Gets a file handle out of an nfs reply sent to the client and returns
3369ec7b004SRick Macklem  * the file handle and the file's attributes.
3379ec7b004SRick Macklem  * For V4, it assumes that Getfh and Getattr Op's results are here.
3389ec7b004SRick Macklem  */
339b9cc3262SRyan Moeller int
nfscl_mtofh(struct nfsrv_descript * nd,struct nfsfh ** nfhpp,struct nfsvattr * nap,int * attrflagp)3409ec7b004SRick Macklem nfscl_mtofh(struct nfsrv_descript *nd, struct nfsfh **nfhpp,
3419ec7b004SRick Macklem     struct nfsvattr *nap, int *attrflagp)
3429ec7b004SRick Macklem {
3439ec7b004SRick Macklem 	u_int32_t *tl;
3449ec7b004SRick Macklem 	int error = 0, flag = 1;
3459ec7b004SRick Macklem 
3469ec7b004SRick Macklem 	*nfhpp = NULL;
3479ec7b004SRick Macklem 	*attrflagp = 0;
3489ec7b004SRick Macklem 	/*
3499ec7b004SRick Macklem 	 * First get the file handle and vnode.
3509ec7b004SRick Macklem 	 */
3519ec7b004SRick Macklem 	if (nd->nd_flag & ND_NFSV3) {
3529ec7b004SRick Macklem 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3539ec7b004SRick Macklem 		flag = fxdr_unsigned(int, *tl);
3549ec7b004SRick Macklem 	} else if (nd->nd_flag & ND_NFSV4) {
3559ec7b004SRick Macklem 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
356e96af294SRick Macklem 		/* If the GetFH failed, clear flag. */
357e96af294SRick Macklem 		if (*++tl != 0) {
358e96af294SRick Macklem 			nd->nd_flag |= ND_NOMOREDATA;
359e96af294SRick Macklem 			flag = 0;
36046adb5dcSRick Macklem 			error = ENXIO;	/* Return ENXIO so *nfhpp isn't used. */
361e96af294SRick Macklem 		}
3629ec7b004SRick Macklem 	}
3639ec7b004SRick Macklem 	if (flag) {
3649ec7b004SRick Macklem 		error = nfsm_getfh(nd, nfhpp);
3659ec7b004SRick Macklem 		if (error)
3669ec7b004SRick Macklem 			return (error);
3679ec7b004SRick Macklem 	}
3689ec7b004SRick Macklem 
3699ec7b004SRick Macklem 	/*
3709ec7b004SRick Macklem 	 * Now, get the attributes.
3719ec7b004SRick Macklem 	 */
372e96af294SRick Macklem 	if (flag != 0 && (nd->nd_flag & ND_NFSV4) != 0) {
3739ec7b004SRick Macklem 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
374e96af294SRick Macklem 		if (*++tl != 0) {
375e96af294SRick Macklem 			nd->nd_flag |= ND_NOMOREDATA;
376e96af294SRick Macklem 			flag = 0;
377e96af294SRick Macklem 		}
3789ec7b004SRick Macklem 	} else if (nd->nd_flag & ND_NFSV3) {
3799ec7b004SRick Macklem 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3809ec7b004SRick Macklem 		if (flag) {
3819ec7b004SRick Macklem 			flag = fxdr_unsigned(int, *tl);
3829ec7b004SRick Macklem 		} else if (fxdr_unsigned(int, *tl)) {
3839ec7b004SRick Macklem 			error = nfsm_advance(nd, NFSX_V3FATTR, -1);
3849ec7b004SRick Macklem 			if (error)
3859ec7b004SRick Macklem 				return (error);
3869ec7b004SRick Macklem 		}
3879ec7b004SRick Macklem 	}
3889ec7b004SRick Macklem 	if (flag) {
3899ec7b004SRick Macklem 		error = nfsm_loadattr(nd, nap);
3909ec7b004SRick Macklem 		if (!error)
3919ec7b004SRick Macklem 			*attrflagp = 1;
3929ec7b004SRick Macklem 	}
3939ec7b004SRick Macklem nfsmout:
3949ec7b004SRick Macklem 	return (error);
3959ec7b004SRick Macklem }
3969ec7b004SRick Macklem 
3979ec7b004SRick Macklem /*
3989ec7b004SRick Macklem  * Initialize the owner/delegation sleep lock.
3999ec7b004SRick Macklem  */
400b9cc3262SRyan Moeller void
nfscl_lockinit(struct nfsv4lock * lckp)4019ec7b004SRick Macklem nfscl_lockinit(struct nfsv4lock *lckp)
4029ec7b004SRick Macklem {
4039ec7b004SRick Macklem 
4049ec7b004SRick Macklem 	lckp->nfslock_usecnt = 0;
4059ec7b004SRick Macklem 	lckp->nfslock_lock = 0;
4069ec7b004SRick Macklem }
4079ec7b004SRick Macklem 
4089ec7b004SRick Macklem /*
4099ec7b004SRick Macklem  * Get an exclusive lock. (Not needed for OpenBSD4, since there is only one
4109ec7b004SRick Macklem  * thread for each posix process in the kernel.)
4119ec7b004SRick Macklem  */
412b9cc3262SRyan Moeller void
nfscl_lockexcl(struct nfsv4lock * lckp,void * mutex)4139ec7b004SRick Macklem nfscl_lockexcl(struct nfsv4lock *lckp, void *mutex)
4149ec7b004SRick Macklem {
4159ec7b004SRick Macklem 	int igotlock;
4169ec7b004SRick Macklem 
4179ec7b004SRick Macklem 	do {
418ff29f3b2SRick Macklem 		igotlock = nfsv4_lock(lckp, 1, NULL, mutex, NULL);
4199ec7b004SRick Macklem 	} while (!igotlock);
4209ec7b004SRick Macklem }
4219ec7b004SRick Macklem 
4229ec7b004SRick Macklem /*
4239ec7b004SRick Macklem  * Release an exclusive lock.
4249ec7b004SRick Macklem  */
425b9cc3262SRyan Moeller void
nfscl_lockunlock(struct nfsv4lock * lckp)4269ec7b004SRick Macklem nfscl_lockunlock(struct nfsv4lock *lckp)
4279ec7b004SRick Macklem {
4289ec7b004SRick Macklem 
4299ec7b004SRick Macklem 	nfsv4_unlock(lckp, 0);
4309ec7b004SRick Macklem }
4319ec7b004SRick Macklem 
4329ec7b004SRick Macklem /*
43390d60ca8SGordon Bergling  * Called to dereference a lock on a stateid (delegation or open owner).
4349ec7b004SRick Macklem  */
435b9cc3262SRyan Moeller void
nfscl_lockderef(struct nfsv4lock * lckp)4369ec7b004SRick Macklem nfscl_lockderef(struct nfsv4lock *lckp)
4379ec7b004SRick Macklem {
4389ec7b004SRick Macklem 
4399ec7b004SRick Macklem 	NFSLOCKCLSTATE();
4409ec7b004SRick Macklem 	lckp->nfslock_usecnt--;
4419ec7b004SRick Macklem 	if (lckp->nfslock_usecnt == 0 && (lckp->nfslock_lock & NFSV4LOCK_WANTED)) {
4429ec7b004SRick Macklem 		lckp->nfslock_lock &= ~NFSV4LOCK_WANTED;
4439ec7b004SRick Macklem 		wakeup((caddr_t)lckp);
4449ec7b004SRick Macklem 	}
4459ec7b004SRick Macklem 	NFSUNLOCKCLSTATE();
4469ec7b004SRick Macklem }
447