nfsm_subs.h revision 1.10
1/*	$NetBSD: nfsm_subs.h,v 1.10 1996/03/20 21:59:56 fvdl Exp $	*/
2
3/*
4 * Copyright (c) 1989, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)nfsm_subs.h	8.2 (Berkeley) 3/30/95
39 */
40
41
42#ifndef _NFS_NFSM_SUBS_H_
43#define _NFS_NFSM_SUBS_H_
44
45
46/*
47 * These macros do strange and peculiar things to mbuf chains for
48 * the assistance of the nfs code. To attempt to use them for any
49 * other purpose will be dangerous. (they make weird assumptions)
50 */
51
52/*
53 * First define what the actual subs. return
54 */
55
56#define	M_HASCL(m)	((m)->m_flags & M_EXT)
57#define	NFSMINOFF(m) \
58		if (M_HASCL(m)) \
59			(m)->m_data = (m)->m_ext.ext_buf; \
60		else if ((m)->m_flags & M_PKTHDR) \
61			(m)->m_data = (m)->m_pktdat; \
62		else \
63			(m)->m_data = (m)->m_dat
64#define	NFSMADV(m, s)	(m)->m_data += (s)
65#define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
66				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
67
68/*
69 * Now for the macros that do the simple stuff and call the functions
70 * for the hard stuff.
71 * These macros use several vars. declared in nfsm_reqhead and these
72 * vars. must not be used elsewhere unless you are careful not to corrupt
73 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
74 * that may be used so long as the value is not expected to retained
75 * after a macro.
76 * I know, this is kind of dorkey, but it makes the actual op functions
77 * fairly clean and deals with the mess caused by the xdr discriminating
78 * unions.
79 */
80
81#define	nfsm_build(a,c,s) \
82		{ if ((s) > M_TRAILINGSPACE(mb)) { \
83			MGET(mb2, M_WAIT, MT_DATA); \
84			if ((s) > MLEN) \
85				panic("build > MLEN"); \
86			mb->m_next = mb2; \
87			mb = mb2; \
88			mb->m_len = 0; \
89			bpos = mtod(mb, caddr_t); \
90		} \
91		(a) = (c)(bpos); \
92		mb->m_len += (s); \
93		bpos += (s); }
94
95#define	nfsm_dissect(a, c, s) \
96		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
97		if (t1 >= (s)) { \
98			(a) = (c)(dpos); \
99			dpos += (s); \
100		} else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
101			error = t1; \
102			m_freem(mrep); \
103			goto nfsmout; \
104		} else { \
105			(a) = (c)cp2; \
106		} }
107
108#define nfsm_fhtom(v, v3) \
109	      { if (v3) { \
110			t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
111			if (t2 <= M_TRAILINGSPACE(mb)) { \
112				nfsm_build(tl, u_int32_t *, t2); \
113				*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
114				*(tl + ((t2>>2) - 2)) = 0; \
115				bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
116					VTONFS(v)->n_fhsize); \
117			} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
118				(caddr_t)VTONFS(v)->n_fhp, \
119				  VTONFS(v)->n_fhsize)) != 0) { \
120				error = t2; \
121				m_freem(mreq); \
122				goto nfsmout; \
123			} \
124		} else { \
125			nfsm_build(cp, caddr_t, NFSX_V2FH); \
126			bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
127		} }
128
129#define nfsm_srvfhtom(f, v3) \
130		{ if (v3) { \
131			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH); \
132			*tl++ = txdr_unsigned(NFSX_V3FH); \
133			bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
134		} else { \
135			nfsm_build(cp, caddr_t, NFSX_V2FH); \
136			bcopy((caddr_t)(f), cp, NFSX_V2FH); \
137		} }
138
139#define nfsm_srvpostop_fh(f) \
140		{ nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
141		*tl++ = nfs_true; \
142		*tl++ = txdr_unsigned(NFSX_V3FH); \
143		bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
144		}
145
146#define nfsm_mtofh(d, v, v3, f) \
147		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
148		if (v3) { \
149			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
150			(f) = fxdr_unsigned(int, *tl); \
151		} else \
152			(f) = 1; \
153		if (f) { \
154			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
155			if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
156				&ttnp)) != 0) { \
157				error = t1; \
158				m_freem(mrep); \
159				goto nfsmout; \
160			} \
161			(v) = NFSTOV(ttnp); \
162		} \
163		if (v3) { \
164			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
165			if (f) \
166				(f) = fxdr_unsigned(int, *tl); \
167			else if (fxdr_unsigned(int, *tl)) \
168				nfsm_adv(NFSX_V3FATTR); \
169		} \
170		if (f) \
171			nfsm_loadattr((v), (struct vattr *)0); \
172		}
173
174#define nfsm_getfh(f, s, v3) \
175		{ if (v3) { \
176			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
177			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
178				(s) > NFSX_V3FHMAX) { \
179				m_freem(mrep); \
180				error = EBADRPC; \
181				goto nfsmout; \
182			} \
183		} else \
184			(s) = NFSX_V2FH; \
185		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
186
187#define	nfsm_loadattr(v, a) \
188		{ struct vnode *ttvp = (v); \
189		if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) != 0) { \
190			error = t1; \
191			m_freem(mrep); \
192			goto nfsmout; \
193		} \
194		(v) = ttvp; }
195
196#define	nfsm_postop_attr(v, f) \
197		{ struct vnode *ttvp = (v); \
198		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
199		if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
200			if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
201				(struct vattr *)0)) != 0) { \
202				error = t1; \
203				(f) = 0; \
204				m_freem(mrep); \
205				goto nfsmout; \
206			} \
207			(v) = ttvp; \
208		} }
209
210/* Used as (f) for nfsm_wcc_data() */
211#define NFSV3_WCCRATTR	0
212#define NFSV3_WCCCHK	1
213
214#define	nfsm_wcc_data(v, f) \
215		{ int ttattrf, ttretf = 0; \
216		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
217		if (*tl == nfs_true) { \
218			nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
219			if (f) \
220				ttretf = (VTONFS(v)->n_mtime == \
221					fxdr_unsigned(u_int32_t, *(tl + 2))); \
222		} \
223		nfsm_postop_attr((v), ttattrf); \
224		if (f) { \
225			(f) = ttretf; \
226		} else { \
227			(f) = ttattrf; \
228		} }
229
230#define nfsm_v3sattr(s, a) \
231		{ (s)->sa_modetrue = nfs_true; \
232		(s)->sa_mode = vtonfsv3_mode((a)->va_mode); \
233		(s)->sa_uidfalse = nfs_false; \
234		(s)->sa_gidfalse = nfs_false; \
235		(s)->sa_sizefalse = nfs_false; \
236		(s)->sa_atimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
237		txdr_nfsv3time(&(a)->va_atime, &(s)->sa_atime); \
238		(s)->sa_mtimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
239		txdr_nfsv3time(&(a)->va_mtime, &(s)->sa_mtime); \
240		}
241
242#define	nfsm_strsiz(s,m) \
243		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
244		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
245			m_freem(mrep); \
246			error = EBADRPC; \
247			goto nfsmout; \
248		} }
249
250#define	nfsm_srvstrsiz(s,m) \
251		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
252		if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
253			error = EBADRPC; \
254			nfsm_reply(0); \
255		} }
256
257#define	nfsm_srvnamesiz(s) \
258		{ nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
259		if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
260			error = NFSERR_NAMETOL; \
261		if ((s) <= 0) \
262			error = EBADRPC; \
263		if (error) \
264			nfsm_reply(0); \
265		}
266
267#define nfsm_mtouio(p,s) \
268		if ((s) > 0 && \
269		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
270			error = t1; \
271			m_freem(mrep); \
272			goto nfsmout; \
273		}
274
275#define nfsm_uiotom(p,s) \
276		if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
277			error = t1; \
278			m_freem(mreq); \
279			goto nfsmout; \
280		}
281
282#define	nfsm_reqhead(v,a,s) \
283		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
284
285#define nfsm_reqdone	m_freem(mrep); \
286		nfsmout:
287
288#define nfsm_rndup(a)	(((a)+3)&(~0x3))
289
290#define	nfsm_request(v, t, p, c)	\
291		if ((error = nfs_request((v), mreq, (t), (p), \
292		   (c), &mrep, &md, &dpos)) != 0) { \
293			if (error & NFSERR_RETERR) \
294				error &= ~NFSERR_RETERR; \
295			else \
296				goto nfsmout; \
297		}
298
299#define	nfsm_strtom(a,s,m) \
300		if ((s) > (m)) { \
301			m_freem(mreq); \
302			error = ENAMETOOLONG; \
303			goto nfsmout; \
304		} \
305		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
306		if (t2 <= M_TRAILINGSPACE(mb)) { \
307			nfsm_build(tl,u_int32_t *,t2); \
308			*tl++ = txdr_unsigned(s); \
309			*(tl+((t2>>2)-2)) = 0; \
310			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
311		} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
312			error = t2; \
313			m_freem(mreq); \
314			goto nfsmout; \
315		}
316
317#define	nfsm_srvdone \
318		nfsmout: \
319		return(error)
320
321#define	nfsm_reply(s) \
322		{ \
323		nfsd->nd_repstat = error; \
324		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
325		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
326			mrq, &mb, &bpos); \
327		else \
328		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
329			mrq, &mb, &bpos); \
330		if (mrep != NULL) { \
331			m_freem(mrep); \
332			mrep = NULL; \
333		} \
334		mreq = *mrq; \
335		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
336			error == EBADRPC)) \
337			return(0); \
338		}
339
340#define	nfsm_writereply(s, v3) \
341		{ \
342		nfsd->nd_repstat = error; \
343		if (error && !(v3)) \
344		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
345			&mreq, &mb, &bpos); \
346		else \
347		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
348			&mreq, &mb, &bpos); \
349		}
350
351#define	nfsm_adv(s) \
352		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
353		if (t1 >= (s)) { \
354			dpos += (s); \
355		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
356			error = t1; \
357			m_freem(mrep); \
358			goto nfsmout; \
359		} }
360
361#define nfsm_srvmtofh(f) \
362		{ if (nfsd->nd_flag & ND_NFSV3) { \
363			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
364			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
365				error = EBADRPC; \
366				nfsm_reply(0); \
367			} \
368		} \
369		nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
370		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
371		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
372			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
373		}
374
375#define	nfsm_clget \
376		if (bp >= be) { \
377			if (mp == mb) \
378				mp->m_len += bp-bpos; \
379			MGET(mp, M_WAIT, MT_DATA); \
380			MCLGET(mp, M_WAIT); \
381			mp->m_len = NFSMSIZ(mp); \
382			mp2->m_next = mp; \
383			mp2 = mp; \
384			bp = mtod(mp, caddr_t); \
385			be = bp+mp->m_len; \
386		} \
387		tl = (u_int32_t *)bp
388
389#define	nfsm_srvfillattr(a, f) \
390		nfsm_srvfattr(nfsd, (a), (f))
391
392#define nfsm_srvwcc_data(br, b, ar, a) \
393		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
394
395#define nfsm_srvpostop_attr(r, a) \
396		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
397
398#define nfsm_srvsattr(a) \
399		{ nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
400		if (*tl == nfs_true) { \
401			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
402			(a)->va_mode = nfstov_mode(*tl); \
403		} \
404		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
405		if (*tl == nfs_true) { \
406			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
407			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
408		} \
409		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
410		if (*tl == nfs_true) { \
411			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
412			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
413		} \
414		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
415		if (*tl == nfs_true) { \
416			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
417			fxdr_hyper(tl, &(a)->va_size); \
418		} \
419		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
420		switch (fxdr_unsigned(int, *tl)) { \
421		case NFSV3SATTRTIME_TOCLIENT: \
422			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
423			fxdr_nfsv3time(tl, &(a)->va_atime); \
424			break; \
425		case NFSV3SATTRTIME_TOSERVER: \
426			(a)->va_atime.tv_sec = time.tv_sec; \
427			(a)->va_atime.tv_nsec = time.tv_usec * 1000; \
428			break; \
429		}; \
430		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
431		switch (fxdr_unsigned(int, *tl)) { \
432		case NFSV3SATTRTIME_TOCLIENT: \
433			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
434			fxdr_nfsv3time(tl, &(a)->va_mtime); \
435			break; \
436		case NFSV3SATTRTIME_TOSERVER: \
437			(a)->va_mtime.tv_sec = time.tv_sec; \
438			(a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
439			break; \
440		}; }
441
442#endif
443