nfsm_subs.h revision 1817
1155192Srwatson/*
2155192Srwatson * Copyright (c) 1989, 1993
3170407Srwatson *	The Regents of the University of California.  All rights reserved.
4155192Srwatson *
5155192Srwatson * This code is derived from software contributed to Berkeley by
6155192Srwatson * Rick Macklem at The University of Guelph.
7155192Srwatson *
8155192Srwatson * Redistribution and use in source and binary forms, with or without
9155192Srwatson * modification, are permitted provided that the following conditions
10155192Srwatson * are met:
11155192Srwatson * 1. Redistributions of source code must retain the above copyright
12155192Srwatson *    notice, this list of conditions and the following disclaimer.
13155192Srwatson * 2. Redistributions in binary form must reproduce the above copyright
14155192Srwatson *    notice, this list of conditions and the following disclaimer in the
15155192Srwatson *    documentation and/or other materials provided with the distribution.
16155192Srwatson * 3. All advertising materials mentioning features or use of this software
17155192Srwatson *    must display the following acknowledgement:
18155192Srwatson *	This product includes software developed by the University of
19155192Srwatson *	California, Berkeley and its contributors.
20155192Srwatson * 4. Neither the name of the University nor the names of its contributors
21155192Srwatson *    may be used to endorse or promote products derived from this software
22155192Srwatson *    without specific prior written permission.
23155192Srwatson *
24155192Srwatson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25155192Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26155192Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27155192Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28155192Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29155192Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30155192Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31155192Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32155192Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33155192Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34155192Srwatson * SUCH DAMAGE.
35155192Srwatson *
36155192Srwatson *	@(#)nfsm_subs.h	8.1 (Berkeley) 6/16/93
37155192Srwatson * $Id$
38155192Srwatson */
39155192Srwatson
40155192Srwatson/*
41155192Srwatson * These macros do strange and peculiar things to mbuf chains for
42155192Srwatson * the assistance of the nfs code. To attempt to use them for any
43155192Srwatson * other purpose will be dangerous. (they make weird assumptions)
44155192Srwatson */
45164033Srwatson
46155192Srwatson/*
47155192Srwatson * First define what the actual subs. return
48155192Srwatson */
49155192Srwatsonextern struct mbuf *nfsm_reqh();
50155192Srwatson
51155192Srwatson#define	M_HASCL(m)	((m)->m_flags & M_EXT)
52171144Srwatson#define	NFSMINOFF(m) \
53155192Srwatson		if (M_HASCL(m)) \
54155192Srwatson			(m)->m_data = (m)->m_ext.ext_buf; \
55155192Srwatson		else if ((m)->m_flags & M_PKTHDR) \
56155192Srwatson			(m)->m_data = (m)->m_pktdat; \
57155192Srwatson		else \
58155192Srwatson			(m)->m_data = (m)->m_dat
59155192Srwatson#define	NFSMADV(m, s)	(m)->m_data += (s)
60155192Srwatson#define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
61155192Srwatson				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
62155406Srwatson
63156291Srwatson/*
64155406Srwatson * Now for the macros that do the simple stuff and call the functions
65155406Srwatson * for the hard stuff.
66155192Srwatson * These macros use several vars. declared in nfsm_reqhead and these
67155192Srwatson * vars. must not be used elsewhere unless you are careful not to corrupt
68155192Srwatson * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
69155192Srwatson * that may be used so long as the value is not expected to retained
70155192Srwatson * after a macro.
71155192Srwatson * I know, this is kind of dorkey, but it makes the actual op functions
72155406Srwatson * fairly clean and deals with the mess caused by the xdr discriminating
73155406Srwatson * unions.
74156888Srwatson */
75170407Srwatson
76155192Srwatson#define	nfsm_build(a,c,s) \
77155192Srwatson		{ if ((s) > M_TRAILINGSPACE(mb)) { \
78155192Srwatson			MGET(mb2, M_WAIT, MT_DATA); \
79155192Srwatson			if ((s) > MLEN) \
80171144Srwatson				panic("build > MLEN"); \
81171144Srwatson			mb->m_next = mb2; \
82171144Srwatson			mb = mb2; \
83155192Srwatson			mb->m_len = 0; \
84170196Srwatson			bpos = mtod(mb, caddr_t); \
85170196Srwatson		} \
86170196Srwatson		(a) = (c)(bpos); \
87155192Srwatson		mb->m_len += (s); \
88155192Srwatson		bpos += (s); }
89156889Srwatson
90156889Srwatson#define	nfsm_dissect(a,c,s) \
91155192Srwatson		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
92155192Srwatson		if (t1 >= (s)) { \
93162176Srwatson			(a) = (c)(dpos); \
94162176Srwatson			dpos += (s); \
95155192Srwatson		} else if (error = nfsm_disct(&md, &dpos, (s), t1, &cp2)) { \
96156889Srwatson			m_freem(mrep); \
97156889Srwatson			goto nfsmout; \
98161813Swsalamon		} else { \
99161813Swsalamon			(a) = (c)cp2; \
100155192Srwatson		} }
101155192Srwatson
102155192Srwatson#define nfsm_fhtom(v) \
103155192Srwatson		nfsm_build(cp,caddr_t,NFSX_FH); \
104156889Srwatson		bcopy((caddr_t)&(VTONFS(v)->n_fh), cp, NFSX_FH)
105155192Srwatson
106155192Srwatson#define nfsm_srvfhtom(f) \
107170691Srwatson		nfsm_build(cp,caddr_t,NFSX_FH); \
108155192Srwatson		bcopy((caddr_t)(f), cp, NFSX_FH)
109156889Srwatson
110155192Srwatson#define nfsm_mtofh(d,v) \
111155192Srwatson		{ struct nfsnode *np; nfsv2fh_t *fhp; \
112155192Srwatson		nfsm_dissect(fhp,nfsv2fh_t *,NFSX_FH); \
113155192Srwatson		if (error = nfs_nget((d)->v_mount, fhp, &np)) { \
114156889Srwatson			m_freem(mrep); \
115155192Srwatson			goto nfsmout; \
116155192Srwatson		} \
117155192Srwatson		(v) = NFSTOV(np); \
118155192Srwatson		nfsm_loadattr(v, (struct vattr *)0); \
119155192Srwatson		}
120156889Srwatson
121155192Srwatson#define	nfsm_loadattr(v,a) \
122155192Srwatson		{ struct vnode *tvp = (v); \
123170196Srwatson		if (error = nfs_loadattrcache(&tvp, &md, &dpos, (a))) { \
124170196Srwatson			m_freem(mrep); \
125170196Srwatson			goto nfsmout; \
126170196Srwatson		} \
127170196Srwatson		(v) = tvp; }
128170196Srwatson
129155192Srwatson#define	nfsm_strsiz(s,m) \
130156889Srwatson		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
131156889Srwatson		if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \
132156889Srwatson			m_freem(mrep); \
133155192Srwatson			error = EBADRPC; \
134155192Srwatson			goto nfsmout; \
135155192Srwatson		} }
136155192Srwatson
137156889Srwatson#define	nfsm_srvstrsiz(s,m) \
138155192Srwatson		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
139155192Srwatson		if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
140170196Srwatson			error = EBADRPC; \
141170196Srwatson			nfsm_reply(0); \
142155192Srwatson		} }
143159261Srwatson
144155192Srwatson#define nfsm_mtouio(p,s) \
145155192Srwatson		if ((s) > 0 && \
146159261Srwatson		   (error = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
147159261Srwatson			m_freem(mrep); \
148159261Srwatson			goto nfsmout; \
149155192Srwatson		}
150159261Srwatson
151155192Srwatson#define nfsm_uiotom(p,s) \
152156889Srwatson		if (error = nfsm_uiotombuf((p),&mb,(s),&bpos)) { \
153156889Srwatson			m_freem(mreq); \
154170196Srwatson			goto nfsmout; \
155170196Srwatson		}
156155192Srwatson
157156889Srwatson#define	nfsm_reqhead(v,a,s) \
158155192Srwatson		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
159155192Srwatson
160155406Srwatson#define nfsm_reqdone	m_freem(mrep); \
161155192Srwatson		nfsmout:
162155406Srwatson
163155406Srwatson#define nfsm_rndup(a)	(((a)+3)&(~0x3))
164155406Srwatson
165155406Srwatson#define	nfsm_request(v, t, p, c)	\
166155406Srwatson		if (error = nfs_request((v), mreq, (t), (p), \
167155406Srwatson		   (c), &mrep, &md, &dpos)) \
168155406Srwatson			goto nfsmout
169155406Srwatson
170155406Srwatson#define	nfsm_strtom(a,s,m) \
171155406Srwatson		if ((s) > (m)) { \
172155406Srwatson			m_freem(mreq); \
173155406Srwatson			error = ENAMETOOLONG; \
174155406Srwatson			goto nfsmout; \
175155406Srwatson		} \
176155406Srwatson		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
177155406Srwatson		if (t2 <= M_TRAILINGSPACE(mb)) { \
178155406Srwatson			nfsm_build(tl,u_long *,t2); \
179155406Srwatson			*tl++ = txdr_unsigned(s); \
180155406Srwatson			*(tl+((t2>>2)-2)) = 0; \
181155406Srwatson			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
182155406Srwatson		} else if (error = nfsm_strtmbuf(&mb, &bpos, (a), (s))) { \
183170407Srwatson			m_freem(mreq); \
184170407Srwatson			goto nfsmout; \
185155406Srwatson		}
186170407Srwatson
187170407Srwatson#define	nfsm_srvdone \
188155406Srwatson		nfsmout: \
189155406Srwatson		return(error)
190155406Srwatson
191155192Srwatson#define	nfsm_reply(s) \
192155406Srwatson		{ \
193155192Srwatson		nfsd->nd_repstat = error; \
194155406Srwatson		if (error) \
195155192Srwatson		   (void) nfs_rephead(0, nfsd, error, cache, &frev, \
196155406Srwatson			mrq, &mb, &bpos); \
197155406Srwatson		else \
198155406Srwatson		   (void) nfs_rephead((s), nfsd, error, cache, &frev, \
199155406Srwatson			mrq, &mb, &bpos); \
200155192Srwatson		m_freem(mrep); \
201155406Srwatson		mreq = *mrq; \
202155192Srwatson		if (error) \
203155406Srwatson			return(0); \
204155192Srwatson		}
205155406Srwatson
206155192Srwatson#define	nfsm_adv(s) \
207161813Swsalamon		t1 = mtod(md, caddr_t)+md->m_len-dpos; \
208161813Swsalamon		if (t1 >= (s)) { \
209161813Swsalamon			dpos += (s); \
210161813Swsalamon		} else if (error = nfs_adv(&md, &dpos, (s), t1)) { \
211155192Srwatson			m_freem(mrep); \
212155192Srwatson			goto nfsmout; \
213155192Srwatson		}
214155192Srwatson
215155192Srwatson#define nfsm_srvmtofh(f) \
216155192Srwatson		nfsm_dissect(tl, u_long *, NFSX_FH); \
217155192Srwatson		bcopy((caddr_t)tl, (caddr_t)f, NFSX_FH)
218155192Srwatson
219155192Srwatson#define	nfsm_clget \
220155192Srwatson		if (bp >= be) { \
221155192Srwatson			if (mp == mb) \
222155192Srwatson				mp->m_len += bp-bpos; \
223155192Srwatson			MGET(mp, M_WAIT, MT_DATA); \
224155192Srwatson			MCLGET(mp, M_WAIT); \
225155192Srwatson			mp->m_len = NFSMSIZ(mp); \
226155192Srwatson			mp2->m_next = mp; \
227161813Swsalamon			mp2 = mp; \
228161813Swsalamon			bp = mtod(mp, caddr_t); \
229155192Srwatson			be = bp+mp->m_len; \
230170196Srwatson		} \
231156889Srwatson		tl = (u_long *)bp
232173142Srwatson
233173142Srwatson#define	nfsm_srvfillattr \
234155192Srwatson	fp->fa_type = vtonfs_type(vap->va_type); \
235155192Srwatson	fp->fa_mode = vtonfs_mode(vap->va_type, vap->va_mode); \
236155192Srwatson	fp->fa_nlink = txdr_unsigned(vap->va_nlink); \
237155192Srwatson	fp->fa_uid = txdr_unsigned(vap->va_uid); \
238155192Srwatson	fp->fa_gid = txdr_unsigned(vap->va_gid); \
239155192Srwatson	if (nfsd->nd_nqlflag == NQL_NOVAL) { \
240155192Srwatson		fp->fa_nfsblocksize = txdr_unsigned(vap->va_blocksize); \
241155192Srwatson		if (vap->va_type == VFIFO) \
242155192Srwatson			fp->fa_nfsrdev = 0xffffffff; \
243155192Srwatson		else \
244159261Srwatson			fp->fa_nfsrdev = txdr_unsigned(vap->va_rdev); \
245159261Srwatson		fp->fa_nfsfsid = txdr_unsigned(vap->va_fsid); \
246155192Srwatson		fp->fa_nfsfileid = txdr_unsigned(vap->va_fileid); \
247155192Srwatson		fp->fa_nfssize = txdr_unsigned(vap->va_size); \
248159266Srwatson		fp->fa_nfsblocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); \
249155406Srwatson		txdr_nfstime(&vap->va_atime, &fp->fa_nfsatime); \
250155406Srwatson		txdr_nfstime(&vap->va_mtime, &fp->fa_nfsmtime); \
251155406Srwatson		fp->fa_nfsctime.nfs_sec = txdr_unsigned(vap->va_ctime.ts_sec); \
252155192Srwatson		fp->fa_nfsctime.nfs_usec = txdr_unsigned(vap->va_gen); \
253155192Srwatson	} else { \
254155192Srwatson		fp->fa_nqblocksize = txdr_unsigned(vap->va_blocksize); \
255155192Srwatson		if (vap->va_type == VFIFO) \
256155192Srwatson			fp->fa_nqrdev = 0xffffffff; \
257155192Srwatson		else \
258155192Srwatson			fp->fa_nqrdev = txdr_unsigned(vap->va_rdev); \
259155192Srwatson		fp->fa_nqfsid = txdr_unsigned(vap->va_fsid); \
260155192Srwatson		fp->fa_nqfileid = txdr_unsigned(vap->va_fileid); \
261156888Srwatson		txdr_hyper(&vap->va_size, &fp->fa_nqsize); \
262156888Srwatson		txdr_hyper(&vap->va_bytes, &fp->fa_nqbytes); \
263155192Srwatson		txdr_nqtime(&vap->va_atime, &fp->fa_nqatime); \
264155192Srwatson		txdr_nqtime(&vap->va_mtime, &fp->fa_nqmtime); \
265155192Srwatson		txdr_nqtime(&vap->va_ctime, &fp->fa_nqctime); \
266155192Srwatson		fp->fa_nqflags = txdr_unsigned(vap->va_flags); \
267155192Srwatson		fp->fa_nqgen = txdr_unsigned(vap->va_gen); \
268155192Srwatson		txdr_hyper(&vap->va_filerev, &fp->fa_nqfilerev); \
269155192Srwatson	}
270155192Srwatson
271155192Srwatson