nfs_common.h revision 1817
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$
38 */
39
40/*
41 * These macros do strange and peculiar things to mbuf chains for
42 * the assistance of the nfs code. To attempt to use them for any
43 * other purpose will be dangerous. (they make weird assumptions)
44 */
45
46/*
47 * First define what the actual subs. return
48 */
49extern struct mbuf *nfsm_reqh();
50
51#define	M_HASCL(m)	((m)->m_flags & M_EXT)
52#define	NFSMINOFF(m) \
53		if (M_HASCL(m)) \
54			(m)->m_data = (m)->m_ext.ext_buf; \
55		else if ((m)->m_flags & M_PKTHDR) \
56			(m)->m_data = (m)->m_pktdat; \
57		else \
58			(m)->m_data = (m)->m_dat
59#define	NFSMADV(m, s)	(m)->m_data += (s)
60#define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
61				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
62
63/*
64 * Now for the macros that do the simple stuff and call the functions
65 * for the hard stuff.
66 * These macros use several vars. declared in nfsm_reqhead and these
67 * vars. must not be used elsewhere unless you are careful not to corrupt
68 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
69 * that may be used so long as the value is not expected to retained
70 * after a macro.
71 * I know, this is kind of dorkey, but it makes the actual op functions
72 * fairly clean and deals with the mess caused by the xdr discriminating
73 * unions.
74 */
75
76#define	nfsm_build(a,c,s) \
77		{ if ((s) > M_TRAILINGSPACE(mb)) { \
78			MGET(mb2, M_WAIT, MT_DATA); \
79			if ((s) > MLEN) \
80				panic("build > MLEN"); \
81			mb->m_next = mb2; \
82			mb = mb2; \
83			mb->m_len = 0; \
84			bpos = mtod(mb, caddr_t); \
85		} \
86		(a) = (c)(bpos); \
87		mb->m_len += (s); \
88		bpos += (s); }
89
90#define	nfsm_dissect(a,c,s) \
91		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
92		if (t1 >= (s)) { \
93			(a) = (c)(dpos); \
94			dpos += (s); \
95		} else if (error = nfsm_disct(&md, &dpos, (s), t1, &cp2)) { \
96			m_freem(mrep); \
97			goto nfsmout; \
98		} else { \
99			(a) = (c)cp2; \
100		} }
101
102#define nfsm_fhtom(v) \
103		nfsm_build(cp,caddr_t,NFSX_FH); \
104		bcopy((caddr_t)&(VTONFS(v)->n_fh), cp, NFSX_FH)
105
106#define nfsm_srvfhtom(f) \
107		nfsm_build(cp,caddr_t,NFSX_FH); \
108		bcopy((caddr_t)(f), cp, NFSX_FH)
109
110#define nfsm_mtofh(d,v) \
111		{ struct nfsnode *np; nfsv2fh_t *fhp; \
112		nfsm_dissect(fhp,nfsv2fh_t *,NFSX_FH); \
113		if (error = nfs_nget((d)->v_mount, fhp, &np)) { \
114			m_freem(mrep); \
115			goto nfsmout; \
116		} \
117		(v) = NFSTOV(np); \
118		nfsm_loadattr(v, (struct vattr *)0); \
119		}
120
121#define	nfsm_loadattr(v,a) \
122		{ struct vnode *tvp = (v); \
123		if (error = nfs_loadattrcache(&tvp, &md, &dpos, (a))) { \
124			m_freem(mrep); \
125			goto nfsmout; \
126		} \
127		(v) = tvp; }
128
129#define	nfsm_strsiz(s,m) \
130		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
131		if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \
132			m_freem(mrep); \
133			error = EBADRPC; \
134			goto nfsmout; \
135		} }
136
137#define	nfsm_srvstrsiz(s,m) \
138		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
139		if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
140			error = EBADRPC; \
141			nfsm_reply(0); \
142		} }
143
144#define nfsm_mtouio(p,s) \
145		if ((s) > 0 && \
146		   (error = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
147			m_freem(mrep); \
148			goto nfsmout; \
149		}
150
151#define nfsm_uiotom(p,s) \
152		if (error = nfsm_uiotombuf((p),&mb,(s),&bpos)) { \
153			m_freem(mreq); \
154			goto nfsmout; \
155		}
156
157#define	nfsm_reqhead(v,a,s) \
158		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
159
160#define nfsm_reqdone	m_freem(mrep); \
161		nfsmout:
162
163#define nfsm_rndup(a)	(((a)+3)&(~0x3))
164
165#define	nfsm_request(v, t, p, c)	\
166		if (error = nfs_request((v), mreq, (t), (p), \
167		   (c), &mrep, &md, &dpos)) \
168			goto nfsmout
169
170#define	nfsm_strtom(a,s,m) \
171		if ((s) > (m)) { \
172			m_freem(mreq); \
173			error = ENAMETOOLONG; \
174			goto nfsmout; \
175		} \
176		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
177		if (t2 <= M_TRAILINGSPACE(mb)) { \
178			nfsm_build(tl,u_long *,t2); \
179			*tl++ = txdr_unsigned(s); \
180			*(tl+((t2>>2)-2)) = 0; \
181			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
182		} else if (error = nfsm_strtmbuf(&mb, &bpos, (a), (s))) { \
183			m_freem(mreq); \
184			goto nfsmout; \
185		}
186
187#define	nfsm_srvdone \
188		nfsmout: \
189		return(error)
190
191#define	nfsm_reply(s) \
192		{ \
193		nfsd->nd_repstat = error; \
194		if (error) \
195		   (void) nfs_rephead(0, nfsd, error, cache, &frev, \
196			mrq, &mb, &bpos); \
197		else \
198		   (void) nfs_rephead((s), nfsd, error, cache, &frev, \
199			mrq, &mb, &bpos); \
200		m_freem(mrep); \
201		mreq = *mrq; \
202		if (error) \
203			return(0); \
204		}
205
206#define	nfsm_adv(s) \
207		t1 = mtod(md, caddr_t)+md->m_len-dpos; \
208		if (t1 >= (s)) { \
209			dpos += (s); \
210		} else if (error = nfs_adv(&md, &dpos, (s), t1)) { \
211			m_freem(mrep); \
212			goto nfsmout; \
213		}
214
215#define nfsm_srvmtofh(f) \
216		nfsm_dissect(tl, u_long *, NFSX_FH); \
217		bcopy((caddr_t)tl, (caddr_t)f, NFSX_FH)
218
219#define	nfsm_clget \
220		if (bp >= be) { \
221			if (mp == mb) \
222				mp->m_len += bp-bpos; \
223			MGET(mp, M_WAIT, MT_DATA); \
224			MCLGET(mp, M_WAIT); \
225			mp->m_len = NFSMSIZ(mp); \
226			mp2->m_next = mp; \
227			mp2 = mp; \
228			bp = mtod(mp, caddr_t); \
229			be = bp+mp->m_len; \
230		} \
231		tl = (u_long *)bp
232
233#define	nfsm_srvfillattr \
234	fp->fa_type = vtonfs_type(vap->va_type); \
235	fp->fa_mode = vtonfs_mode(vap->va_type, vap->va_mode); \
236	fp->fa_nlink = txdr_unsigned(vap->va_nlink); \
237	fp->fa_uid = txdr_unsigned(vap->va_uid); \
238	fp->fa_gid = txdr_unsigned(vap->va_gid); \
239	if (nfsd->nd_nqlflag == NQL_NOVAL) { \
240		fp->fa_nfsblocksize = txdr_unsigned(vap->va_blocksize); \
241		if (vap->va_type == VFIFO) \
242			fp->fa_nfsrdev = 0xffffffff; \
243		else \
244			fp->fa_nfsrdev = txdr_unsigned(vap->va_rdev); \
245		fp->fa_nfsfsid = txdr_unsigned(vap->va_fsid); \
246		fp->fa_nfsfileid = txdr_unsigned(vap->va_fileid); \
247		fp->fa_nfssize = txdr_unsigned(vap->va_size); \
248		fp->fa_nfsblocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE); \
249		txdr_nfstime(&vap->va_atime, &fp->fa_nfsatime); \
250		txdr_nfstime(&vap->va_mtime, &fp->fa_nfsmtime); \
251		fp->fa_nfsctime.nfs_sec = txdr_unsigned(vap->va_ctime.ts_sec); \
252		fp->fa_nfsctime.nfs_usec = txdr_unsigned(vap->va_gen); \
253	} else { \
254		fp->fa_nqblocksize = txdr_unsigned(vap->va_blocksize); \
255		if (vap->va_type == VFIFO) \
256			fp->fa_nqrdev = 0xffffffff; \
257		else \
258			fp->fa_nqrdev = txdr_unsigned(vap->va_rdev); \
259		fp->fa_nqfsid = txdr_unsigned(vap->va_fsid); \
260		fp->fa_nqfileid = txdr_unsigned(vap->va_fileid); \
261		txdr_hyper(&vap->va_size, &fp->fa_nqsize); \
262		txdr_hyper(&vap->va_bytes, &fp->fa_nqbytes); \
263		txdr_nqtime(&vap->va_atime, &fp->fa_nqatime); \
264		txdr_nqtime(&vap->va_mtime, &fp->fa_nqmtime); \
265		txdr_nqtime(&vap->va_ctime, &fp->fa_nqctime); \
266		fp->fa_nqflags = txdr_unsigned(vap->va_flags); \
267		fp->fa_nqgen = txdr_unsigned(vap->va_gen); \
268		txdr_hyper(&vap->va_filerev, &fp->fa_nqfilerev); \
269	}
270
271