nfsm_subs.h revision 1.30
1/*	$NetBSD: nfsm_subs.h,v 1.30 2003/06/29 22:32:21 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)) ? (m)->m_ext.ext_size : \
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			struct mbuf *mb2; \
84			mb2 = m_get(M_WAIT, MT_DATA); \
85			MCLAIM(mb2, &nfs_mowner); \
86			if ((s) > MLEN) \
87				panic("build > MLEN"); \
88			mb->m_next = mb2; \
89			mb = mb2; \
90			mb->m_len = 0; \
91			bpos = mtod(mb, caddr_t); \
92		} \
93		(a) = (c)(bpos); \
94		mb->m_len += (s); \
95		bpos += (s); }
96
97#define nfsm_aligned(p) ALIGNED_POINTER(p,u_int32_t)
98
99#define	nfsm_dissect(a, c, s) \
100		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
101		if (t1 >= (s) && nfsm_aligned(dpos)) { \
102			(a) = (c)(dpos); \
103			dpos += (s); \
104		} else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
105			error = t1; \
106			m_freem(mrep); \
107			goto nfsmout; \
108		} else { \
109			(a) = (c)cp2; \
110		} }
111
112#define nfsm_fhtom(n, v3) \
113	      { if (v3) { \
114			t2 = nfsm_rndup((n)->n_fhsize) + NFSX_UNSIGNED; \
115			if (t2 <= M_TRAILINGSPACE(mb)) { \
116				nfsm_build(tl, u_int32_t *, t2); \
117				*tl++ = txdr_unsigned((n)->n_fhsize); \
118				*(tl + ((t2>>2) - 2)) = 0; \
119				memcpy((caddr_t)tl,(caddr_t)(n)->n_fhp, \
120					(n)->n_fhsize); \
121			} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
122				(caddr_t)(n)->n_fhp, \
123				  (n)->n_fhsize)) != 0) { \
124				error = t2; \
125				m_freem(mreq); \
126				goto nfsmout; \
127			} \
128		} else { \
129			nfsm_build(cp, caddr_t, NFSX_V2FH); \
130			memcpy(cp, (caddr_t)(n)->n_fhp, NFSX_V2FH); \
131		} }
132
133#define nfsm_srvfhtom(f, v3) \
134		{ if (v3) { \
135			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH); \
136			*tl++ = txdr_unsigned(NFSX_V3FH); \
137			memcpy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
138		} else { \
139			nfsm_build(cp, caddr_t, NFSX_V2FH); \
140			memcpy(cp, (caddr_t)(f), NFSX_V2FH); \
141		} }
142
143#define nfsm_srvpostop_fh(f) \
144		{ nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
145		*tl++ = nfs_true; \
146		*tl++ = txdr_unsigned(NFSX_V3FH); \
147		memcpy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
148		}
149
150#define nfsm_mtofh(d, v, v3, f) \
151		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
152		if (v3) { \
153			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
154			(f) = fxdr_unsigned(int, *tl); \
155		} else \
156			(f) = 1; \
157		if (f) { \
158			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
159			if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
160				&ttnp)) != 0) { \
161				error = t1; \
162				m_freem(mrep); \
163				goto nfsmout; \
164			} \
165			(v) = NFSTOV(ttnp); \
166		} \
167		if (v3) { \
168			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
169			if (f) \
170				(f) = fxdr_unsigned(int, *tl); \
171			else if (fxdr_unsigned(int, *tl)) \
172				nfsm_adv(NFSX_V3FATTR); \
173		} \
174		if (f) \
175			nfsm_loadattr((v), (struct vattr *)0, 0); \
176		}
177
178#define nfsm_getfh(f, s, v3) \
179		{ if (v3) { \
180			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
181			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
182				(s) > NFSX_V3FHMAX) { \
183				m_freem(mrep); \
184				error = EBADRPC; \
185				goto nfsmout; \
186			} \
187		} else \
188			(s) = NFSX_V2FH; \
189		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
190
191#define	nfsm_loadattr(v, a, flags) \
192		{ struct vnode *ttvp = (v); \
193		if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, (a), (flags))) \
194		    != 0) { \
195			error = t1; \
196			m_freem(mrep); \
197			goto nfsmout; \
198		} \
199		(v) = ttvp; }
200
201#define	nfsm_postop_attr(v, f, flags) \
202		{ struct vnode *ttvp = (v); \
203		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
204		if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
205			if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, \
206				(struct vattr *)0, (flags))) != 0) { \
207				error = t1; \
208				(f) = 0; \
209				m_freem(mrep); \
210				goto nfsmout; \
211			} \
212			(v) = ttvp; \
213		} }
214
215/* Used as (f) for nfsm_wcc_data() */
216#define NFSV3_WCCRATTR	0
217#define NFSV3_WCCCHK	1
218
219#define	nfsm_wcc_data(v, f, flags) \
220		{ int ttattrf, ttretf = 0; \
221		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
222		if (*tl == nfs_true) { \
223			nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
224			if (f) \
225				ttretf = (VTONFS(v)->n_mtime == \
226					fxdr_unsigned(u_int32_t, *(tl + 2))); \
227		} \
228		nfsm_postop_attr((v), ttattrf, (flags)); \
229		if (f) { \
230			(f) = ttretf; \
231		} else { \
232			(f) = ttattrf; \
233		} }
234
235/* If full is true, set all fields, otherwise just set mode and time fields */
236#define nfsm_v3attrbuild(a, full)						\
237		{ if ((a)->va_mode != (mode_t)VNOVAL) {				\
238			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);		\
239			*tl++ = nfs_true;					\
240			*tl = txdr_unsigned((a)->va_mode);			\
241		} else {							\
242			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
243			*tl = nfs_false;					\
244		}								\
245		if ((full) && (a)->va_uid != (uid_t)VNOVAL) {			\
246			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);		\
247			*tl++ = nfs_true;					\
248			*tl = txdr_unsigned((a)->va_uid);			\
249		} else {							\
250			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
251			*tl = nfs_false;					\
252		}								\
253		if ((full) && (a)->va_gid != (gid_t)VNOVAL) {			\
254			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);		\
255			*tl++ = nfs_true;					\
256			*tl = txdr_unsigned((a)->va_gid);			\
257		} else {							\
258			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
259			*tl = nfs_false;					\
260		}								\
261		if ((full) && (a)->va_size != VNOVAL) {				\
262			nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);		\
263			*tl++ = nfs_true;					\
264			txdr_hyper((a)->va_size, tl);				\
265		} else {							\
266			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
267			*tl = nfs_false;					\
268		}								\
269		if ((a)->va_atime.tv_sec != VNOVAL) {				\
270			if ((a)->va_atime.tv_sec != time.tv_sec) {		\
271				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);	\
272				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);	\
273				txdr_nfsv3time(&(a)->va_atime, tl);		\
274			} else {						\
275				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);	\
276				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);	\
277			}							\
278		} else {							\
279			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
280			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);		\
281		}								\
282		if ((a)->va_mtime.tv_sec != VNOVAL) {				\
283			if ((a)->va_mtime.tv_sec != time.tv_sec) {		\
284				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);	\
285				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);	\
286				txdr_nfsv3time(&(a)->va_mtime, tl);		\
287			} else {						\
288				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);	\
289				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);	\
290			}							\
291		} else {							\
292			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\
293			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);		\
294		}								\
295		}
296
297
298#define	nfsm_strsiz(s,m) \
299		{ nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \
300		if (((s) = fxdr_unsigned(uint32_t,*tl)) > (m)) { \
301			m_freem(mrep); \
302			error = EBADRPC; \
303			goto nfsmout; \
304		} }
305
306#define	nfsm_srvnamesiz(s) \
307		{ nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \
308		if (((s) = fxdr_unsigned(uint32_t,*tl)) > NFS_MAXNAMLEN) \
309			error = NFSERR_NAMETOL; \
310		if (error) \
311			nfsm_reply(0); \
312		}
313
314#define nfsm_mtouio(p,s) \
315		if ((s) > 0 && \
316		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
317			error = t1; \
318			m_freem(mrep); \
319			goto nfsmout; \
320		}
321
322#define nfsm_uiotom(p,s) \
323		if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
324			error = t1; \
325			m_freem(mreq); \
326			goto nfsmout; \
327		}
328
329#define	nfsm_reqhead(n,a,s) \
330		mb = mreq = nfsm_reqh((n),(a),(s),&bpos)
331
332#define nfsm_reqdone	m_freem(mrep); \
333		nfsmout:
334
335#define nfsm_rndup(a)	(((a)+3)&(~0x3))
336#define nfsm_padlen(a)	(nfsm_rndup(a) - (a))
337
338#define	nfsm_request(v, t, p, c)	\
339		if ((error = nfs_request((v), mreq, (t), (p), \
340		   (c), &mrep, &md, &dpos)) != 0) { \
341			if (error & NFSERR_RETERR) \
342				error &= ~NFSERR_RETERR; \
343			else \
344				goto nfsmout; \
345		}
346
347#define	nfsm_strtom(a,s,m) \
348		if ((s) > (m)) { \
349			m_freem(mreq); \
350			error = ENAMETOOLONG; \
351			goto nfsmout; \
352		} \
353		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
354		if (t2 <= M_TRAILINGSPACE(mb)) { \
355			nfsm_build(tl,u_int32_t *,t2); \
356			*tl++ = txdr_unsigned(s); \
357			*(tl+((t2>>2)-2)) = 0; \
358			memcpy((caddr_t)tl, (const char *)(a), (s)); \
359		} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
360			error = t2; \
361			m_freem(mreq); \
362			goto nfsmout; \
363		}
364
365#define	nfsm_srvdone \
366		nfsmout: \
367		return(error)
368
369#define	nfsm_reply(s) \
370		{ \
371		nfsd->nd_repstat = error; \
372		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
373		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
374			mrq, &mb, &bpos); \
375		else \
376		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
377			mrq, &mb, &bpos); \
378		if (mrep != NULL) { \
379			m_freem(mrep); \
380			mrep = NULL; \
381		} \
382		mreq = *mrq; \
383		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
384			error == EBADRPC)) \
385			return(0); \
386		}
387
388#define	nfsm_writereply(s, v3) \
389		{ \
390		nfsd->nd_repstat = error; \
391		if (error && !(v3)) \
392		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
393			&mreq, &mb, &bpos); \
394		else \
395		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
396			&mreq, &mb, &bpos); \
397		}
398
399#define	nfsm_adv(s) \
400		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
401		if (t1 >= (s)) { \
402			dpos += (s); \
403		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
404			error = t1; \
405			m_freem(mrep); \
406			goto nfsmout; \
407		} }
408
409#define nfsm_srvmtofh(f) \
410	{ int fhlen = NFSX_V3FH; \
411		if (nfsd->nd_flag & ND_NFSV3) { \
412			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
413			fhlen = fxdr_unsigned(int, *tl); \
414			if (fhlen == 0) { \
415				memset((caddr_t)(f), 0, NFSX_V3FH); \
416			} else if (fhlen != NFSX_V3FH) { \
417				error = EBADRPC; \
418				nfsm_reply(0); \
419			} \
420		} \
421		if (fhlen != 0) { \
422			nfsm_dissect(tl, u_int32_t *, NFSX_V3FH); \
423			memcpy( (caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
424			if ((nfsd->nd_flag & ND_NFSV3) == 0) \
425				nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
426		} \
427	}
428
429#define	nfsm_clget \
430		if (bp >= be) { \
431			if (mp == mb) \
432				mp->m_len += bp-bpos; \
433			mp = m_get(M_WAIT, MT_DATA); \
434			MCLAIM(mp, &nfs_mowner); \
435			m_clget(mp, M_WAIT); \
436			mp->m_len = NFSMSIZ(mp); \
437			mp2->m_next = mp; \
438			mp2 = mp; \
439			bp = mtod(mp, caddr_t); \
440			be = bp+mp->m_len; \
441		} \
442		tl = (u_int32_t *)bp
443
444#define	nfsm_srvfillattr(a, f) \
445		nfsm_srvfattr(nfsd, (a), (f))
446
447#define nfsm_srvwcc_data(br, b, ar, a) \
448		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
449
450#define nfsm_srvpostop_attr(r, a) \
451		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
452
453#define nfsm_srvsattr(a) \
454		{ nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
455		if (*tl == nfs_true) { \
456			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
457			(a)->va_mode = nfstov_mode(*tl); \
458		} \
459		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
460		if (*tl == nfs_true) { \
461			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
462			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
463		} \
464		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
465		if (*tl == nfs_true) { \
466			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
467			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
468		} \
469		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
470		if (*tl == nfs_true) { \
471			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
472			(a)->va_size = fxdr_hyper(tl); \
473		} \
474		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
475		switch (fxdr_unsigned(int, *tl)) { \
476		case NFSV3SATTRTIME_TOCLIENT: \
477			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
478			fxdr_nfsv3time(tl, &(a)->va_atime); \
479			break; \
480		case NFSV3SATTRTIME_TOSERVER: \
481			(a)->va_atime.tv_sec = time.tv_sec; \
482			(a)->va_atime.tv_nsec = time.tv_usec * 1000; \
483			(a)->va_vaflags |= VA_UTIMES_NULL; \
484			break; \
485		}; \
486		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
487		switch (fxdr_unsigned(int, *tl)) { \
488		case NFSV3SATTRTIME_TOCLIENT: \
489			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
490			fxdr_nfsv3time(tl, &(a)->va_mtime); \
491			(a)->va_vaflags &= ~VA_UTIMES_NULL; \
492			break; \
493		case NFSV3SATTRTIME_TOSERVER: \
494			(a)->va_mtime.tv_sec = time.tv_sec; \
495			(a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
496			(a)->va_vaflags |= VA_UTIMES_NULL; \
497			break; \
498		}; }
499
500#endif
501