nfsm_subs.h revision 3664
1/*
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)nfsm_subs.h	8.1 (Berkeley) 6/16/93
37 * $Id: nfsm_subs.h,v 1.4 1994/10/02 17:27:05 phk Exp $
38 */
39
40#ifndef _NFS_NFSM_SUBS_H_
41#define _NFS_NFSM_SUBS_H_
42
43/*
44 * These macros do strange and peculiar things to mbuf chains for
45 * the assistance of the nfs code. To attempt to use them for any
46 * other purpose will be dangerous. (they make weird assumptions)
47 */
48
49/*
50 * First define what the actual subs. return
51 */
52extern struct mbuf *nfsm_reqh();
53
54#define	M_HASCL(m)	((m)->m_flags & M_EXT)
55#define	NFSMINOFF(m) \
56		if (M_HASCL(m)) \
57			(m)->m_data = (m)->m_ext.ext_buf; \
58		else if ((m)->m_flags & M_PKTHDR) \
59			(m)->m_data = (m)->m_pktdat; \
60		else \
61			(m)->m_data = (m)->m_dat
62#define	NFSMADV(m, s)	(m)->m_data += (s)
63#define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
64				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
65
66/*
67 * Now for the macros that do the simple stuff and call the functions
68 * for the hard stuff.
69 * These macros use several vars. declared in nfsm_reqhead and these
70 * vars. must not be used elsewhere unless you are careful not to corrupt
71 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
72 * that may be used so long as the value is not expected to retained
73 * after a macro.
74 * I know, this is kind of dorkey, but it makes the actual op functions
75 * fairly clean and deals with the mess caused by the xdr discriminating
76 * unions.
77 */
78
79#define	nfsm_build(a,c,s) \
80		{ if ((s) > M_TRAILINGSPACE(mb)) { \
81			MGET(mb2, M_WAIT, MT_DATA); \
82			if ((s) > MLEN) \
83				panic("build > MLEN"); \
84			mb->m_next = mb2; \
85			mb = mb2; \
86			mb->m_len = 0; \
87			bpos = mtod(mb, caddr_t); \
88		} \
89		(a) = (c)(bpos); \
90		mb->m_len += (s); \
91		bpos += (s); }
92
93#define	nfsm_dissect(a,c,s) \
94		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
95		if (t1 >= (s)) { \
96			(a) = (c)(dpos); \
97			dpos += (s); \
98		} else { \
99			error = nfsm_disct(&md, &dpos, (s), t1, &cp2); \
100			if (error) { \
101				m_freem(mrep); \
102				goto nfsmout; \
103			} else { \
104				(a) = (c)cp2; \
105			} \
106		} }
107
108#define nfsm_fhtom(v) \
109		nfsm_build(cp,caddr_t,NFSX_FH); \
110		bcopy((caddr_t)&(VTONFS(v)->n_fh), cp, NFSX_FH)
111
112#define nfsm_srvfhtom(f) \
113		nfsm_build(cp,caddr_t,NFSX_FH); \
114		bcopy((caddr_t)(f), cp, NFSX_FH)
115
116#define nfsm_mtofh(d,v) \
117		{ struct nfsnode *np; nfsv2fh_t *fhp; \
118		nfsm_dissect(fhp,nfsv2fh_t *,NFSX_FH); \
119		error = nfs_nget((d)->v_mount, fhp, &np); \
120		if (error) { \
121			m_freem(mrep); \
122			goto nfsmout; \
123		} \
124		(v) = NFSTOV(np); \
125		nfsm_loadattr(v, (struct vattr *)0); \
126		}
127
128#define	nfsm_loadattr(v,a) \
129		{ struct vnode *tvp = (v); \
130		error = nfs_loadattrcache(&tvp, &md, &dpos, (a)); \
131		if (error) { \
132			m_freem(mrep); \
133			goto nfsmout; \
134		} \
135		(v) = tvp; }
136
137#define	nfsm_strsiz(s,m) \
138		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
139		if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \
140			m_freem(mrep); \
141			error = EBADRPC; \
142			goto nfsmout; \
143		} }
144
145#define	nfsm_srvstrsiz(s,m) \
146		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
147		if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
148			error = EBADRPC; \
149			nfsm_reply(0); \
150		} }
151
152#define nfsm_mtouio(p,s) \
153		if ((s) > 0 && \
154		   (error = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
155			m_freem(mrep); \
156			goto nfsmout; \
157		}
158
159#define nfsm_uiotom(p,s) \
160		error = nfsm_uiotombuf((p),&mb,(s),&bpos); \
161		if (error) { \
162			m_freem(mreq); \
163			goto nfsmout; \
164		}
165
166#define	nfsm_reqhead(v,a,s) \
167		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
168
169#define nfsm_reqdone	m_freem(mrep); \
170		nfsmout:
171
172#define nfsm_rndup(a)	(((a)+3)&(~0x3))
173
174#define	nfsm_request(v, t, p, c)	\
175		error = nfs_request((v), mreq, (t), (p), \
176		   (c), &mrep, &md, &dpos); \
177		if (error) \
178			goto nfsmout
179
180#define	nfsm_strtom(a,s,m) \
181		if ((s) > (m)) { \
182			m_freem(mreq); \
183			error = ENAMETOOLONG; \
184			goto nfsmout; \
185		} \
186		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
187		if (t2 <= M_TRAILINGSPACE(mb)) { \
188			nfsm_build(tl,u_long *,t2); \
189			*tl++ = txdr_unsigned(s); \
190			*(tl+((t2>>2)-2)) = 0; \
191			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
192		} else { \
193			error = nfsm_strtmbuf(&mb, &bpos, (a), (s)); \
194			if (error) { \
195				m_freem(mreq); \
196				goto nfsmout; \
197			} \
198		}
199
200#define	nfsm_srvdone \
201		nfsmout: \
202		return(error)
203
204#define	nfsm_reply(s) \
205		{ \
206		nfsd->nd_repstat = error; \
207		if (error) \
208		   (void) nfs_rephead(0, nfsd, error, cache, &frev, \
209			mrq, &mb, &bpos); \
210		else \
211		   (void) nfs_rephead((s), nfsd, error, cache, &frev, \
212			mrq, &mb, &bpos); \
213		m_freem(mrep); \
214		mreq = *mrq; \
215		if (error) \
216			return(0); \
217		}
218
219#define	nfsm_adv(s) \
220		t1 = mtod(md, caddr_t)+md->m_len-dpos; \
221		if (t1 >= (s)) { \
222			dpos += (s); \
223		} else { \
224			error = nfs_adv(&md, &dpos, (s), t1); \
225			if (error) { \
226				m_freem(mrep); \
227				goto nfsmout; \
228			} \
229		}
230
231#define nfsm_srvmtofh(f) \
232		nfsm_dissect(tl, u_long *, NFSX_FH); \
233		bcopy((caddr_t)tl, (caddr_t)f, NFSX_FH)
234
235#define	nfsm_clget \
236		if (bp >= be) { \
237			if (mp == mb) \
238				mp->m_len += bp-bpos; \
239			MGET(mp, M_WAIT, MT_DATA); \
240			MCLGET(mp, M_WAIT); \
241			mp->m_len = NFSMSIZ(mp); \
242			mp2->m_next = mp; \
243			mp2 = mp; \
244			bp = mtod(mp, caddr_t); \
245			be = bp+mp->m_len; \
246		} \
247		tl = (u_long *)bp
248
249#define	nfsm_srvfillattr \
250	fp->fa_type = vtonfs_type(vap->va_type); \
251	fp->fa_mode = vtonfs_mode(vap->va_type, vap->va_mode); \
252	fp->fa_nlink = txdr_unsigned(vap->va_nlink); \
253	fp->fa_uid = txdr_unsigned(vap->va_uid); \
254	fp->fa_gid = txdr_unsigned(vap->va_gid); \
255	if (nfsd->nd_nqlflag == NQL_NOVAL) { \
256		fp->fa_nfsblocksize = txdr_unsigned(vap->va_blocksize); \
257		if (vap->va_type == VFIFO) \
258			fp->fa_nfsrdev = 0xffffffff; \
259		else \
260			fp->fa_nfsrdev = txdr_unsigned(vap->va_rdev); \
261		fp->fa_nfsfsid = txdr_unsigned(vap->va_fsid); \
262		fp->fa_nfsfileid = txdr_unsigned(vap->va_fileid); \
263		fp->fa_nfssize = txdr_unsigned(vap->va_size); \
264		fp->fa_nfsblocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); \
265		txdr_nfstime(&vap->va_atime, &fp->fa_nfsatime); \
266		txdr_nfstime(&vap->va_mtime, &fp->fa_nfsmtime); \
267		txdr_nfstime(&vap->va_ctime, &fp->fa_nfsctime); \
268	} else { \
269		fp->fa_nqblocksize = txdr_unsigned(vap->va_blocksize); \
270		if (vap->va_type == VFIFO) \
271			fp->fa_nqrdev = 0xffffffff; \
272		else \
273			fp->fa_nqrdev = txdr_unsigned(vap->va_rdev); \
274		fp->fa_nqfsid = txdr_unsigned(vap->va_fsid); \
275		fp->fa_nqfileid = txdr_unsigned(vap->va_fileid); \
276		txdr_hyper(&vap->va_size, &fp->fa_nqsize); \
277		txdr_hyper(&vap->va_bytes, &fp->fa_nqbytes); \
278		txdr_nqtime(&vap->va_atime, &fp->fa_nqatime); \
279		txdr_nqtime(&vap->va_mtime, &fp->fa_nqmtime); \
280		txdr_nqtime(&vap->va_ctime, &fp->fa_nqctime); \
281		fp->fa_nqflags = txdr_unsigned(vap->va_flags); \
282		fp->fa_nqgen = txdr_unsigned(vap->va_gen); \
283		txdr_hyper(&vap->va_filerev, &fp->fa_nqfilerev); \
284	}
285
286#endif
287