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