nfsm_subs.h revision 18397
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.8 1995/08/24 10:45:16 dfr Exp $
38 */
39
40#ifndef _NFS_NFSM_SUBS_H_
41#define _NFS_NFSM_SUBS_H_
42
43
44/*
45 * These macros do strange and peculiar things to mbuf chains for
46 * the assistance of the nfs code. To attempt to use them for any
47 * other purpose will be dangerous. (they make weird assumptions)
48 */
49
50/*
51 * First define what the actual subs. return
52 */
53struct mbuf *nfsm_reqh __P((struct vnode *vp, u_long procid, int hsiz,
54			    caddr_t *bposp));
55struct mbuf *nfsm_rpchead __P((struct ucred *cr, int nmflag, int procid,
56			       int auth_type, int auth_len, char *auth_str,
57			       int verf_len, char *verf_str,
58			       struct mbuf *mrest, int mrest_len,
59			       struct mbuf **mbp, u_long *xidp));
60
61#define	M_HASCL(m)	((m)->m_flags & M_EXT)
62#define	NFSMINOFF(m) \
63		if (M_HASCL(m)) \
64			(m)->m_data = (m)->m_ext.ext_buf; \
65		else if ((m)->m_flags & M_PKTHDR) \
66			(m)->m_data = (m)->m_pktdat; \
67		else \
68			(m)->m_data = (m)->m_dat
69#define	NFSMADV(m, s)	(m)->m_data += (s)
70#define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
71				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
72
73/*
74 * Now for the macros that do the simple stuff and call the functions
75 * for the hard stuff.
76 * These macros use several vars. declared in nfsm_reqhead and these
77 * vars. must not be used elsewhere unless you are careful not to corrupt
78 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
79 * that may be used so long as the value is not expected to retained
80 * after a macro.
81 * I know, this is kind of dorkey, but it makes the actual op functions
82 * fairly clean and deals with the mess caused by the xdr discriminating
83 * unions.
84 */
85
86#define	nfsm_build(a,c,s) \
87		{ if ((s) > M_TRAILINGSPACE(mb)) { \
88			MGET(mb2, M_WAIT, MT_DATA); \
89			if ((s) > MLEN) \
90				panic("build > MLEN"); \
91			mb->m_next = mb2; \
92			mb = mb2; \
93			mb->m_len = 0; \
94			bpos = mtod(mb, caddr_t); \
95		} \
96		(a) = (c)(bpos); \
97		mb->m_len += (s); \
98		bpos += (s); }
99
100#define	nfsm_dissect(a, c, s) \
101		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
102		if (t1 >= (s)) { \
103			(a) = (c)(dpos); \
104			dpos += (s); \
105		} else if (t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) { \
106			error = t1; \
107			m_freem(mrep); \
108			goto nfsmout; \
109		} else { \
110			(a) = (c)cp2; \
111		} }
112
113#define nfsm_fhtom(v, v3) \
114	      { if (v3) { \
115			t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
116			if (t2 <= M_TRAILINGSPACE(mb)) { \
117				nfsm_build(tl, u_long *, t2); \
118				*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
119				*(tl + ((t2>>2) - 2)) = 0; \
120				bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
121					VTONFS(v)->n_fhsize); \
122			} else if (t2 = nfsm_strtmbuf(&mb, &bpos, \
123				(caddr_t)VTONFS(v)->n_fhp, VTONFS(v)->n_fhsize)) { \
124				error = t2; \
125				m_freem(mreq); \
126				goto nfsmout; \
127			} \
128		} else { \
129			nfsm_build(cp, caddr_t, NFSX_V2FH); \
130			bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
131		} }
132
133#define nfsm_srvfhtom(f, v3) \
134		{ if (v3) { \
135			nfsm_build(tl, u_long *, NFSX_UNSIGNED + NFSX_V3FH); \
136			*tl++ = txdr_unsigned(NFSX_V3FH); \
137			bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
138		} else { \
139			nfsm_build(cp, caddr_t, NFSX_V2FH); \
140			bcopy((caddr_t)(f), cp, NFSX_V2FH); \
141		} }
142
143#define nfsm_srvpostop_fh(f) \
144		{ nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
145		*tl++ = nfs_true; \
146		*tl++ = txdr_unsigned(NFSX_V3FH); \
147		bcopy((caddr_t)(f), (caddr_t)tl, 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_long *, 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)) { \
161				error = t1; \
162				m_freem(mrep); \
163				goto nfsmout; \
164			} \
165			(v) = NFSTOV(ttnp); \
166		} \
167		if (v3) { \
168			nfsm_dissect(tl, u_long *, 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); \
176		}
177
178#define nfsm_getfh(f, s, v3) \
179		{ if (v3) { \
180			nfsm_dissect(tl, u_long *, 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) \
192		{ struct vnode *ttvp = (v); \
193		if (t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) { \
194			error = t1; \
195			m_freem(mrep); \
196			goto nfsmout; \
197		} \
198		(v) = ttvp; }
199
200#define	nfsm_postop_attr(v, f) \
201		{ struct vnode *ttvp = (v); \
202		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
203		if ((f) = fxdr_unsigned(int, *tl)) { \
204			if (t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
205				(struct vattr *)0)) { \
206				error = t1; \
207				(f) = 0; \
208				m_freem(mrep); \
209				goto nfsmout; \
210			} \
211			(v) = ttvp; \
212		} }
213
214/* Used as (f) for nfsm_wcc_data() */
215#define NFSV3_WCCRATTR	0
216#define NFSV3_WCCCHK	1
217
218#define	nfsm_wcc_data(v, f) \
219		{ int ttattrf, ttretf = 0; \
220		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
221		if (*tl == nfs_true) { \
222			nfsm_dissect(tl, u_long *, 6 * NFSX_UNSIGNED); \
223			if (f) \
224				ttretf = (VTONFS(v)->n_mtime == \
225					fxdr_unsigned(u_long, *(tl + 2))); \
226		} \
227		nfsm_postop_attr((v), ttattrf); \
228		if (f) { \
229			(f) = ttretf; \
230		} else { \
231			(f) = ttattrf; \
232		} }
233
234#define nfsm_v3sattr(s, a, u, g) \
235		{ (s)->sa_modetrue = nfs_true; \
236		(s)->sa_mode = vtonfsv3_mode((a)->va_mode); \
237		(s)->sa_uidtrue = nfs_true; \
238		(s)->sa_uid = txdr_unsigned(u); \
239		(s)->sa_gidtrue = nfs_true; \
240		(s)->sa_gid = txdr_unsigned(g); \
241		(s)->sa_sizefalse = nfs_false; \
242		(s)->sa_atimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
243		txdr_nfsv3time(&(a)->va_atime, &(s)->sa_atime); \
244		(s)->sa_mtimetype = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
245		txdr_nfsv3time(&(a)->va_mtime, &(s)->sa_mtime); \
246		}
247
248#define	nfsm_strsiz(s,m) \
249		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
250		if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \
251			m_freem(mrep); \
252			error = EBADRPC; \
253			goto nfsmout; \
254		} }
255
256#define	nfsm_srvstrsiz(s,m) \
257		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
258		if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
259			error = EBADRPC; \
260			nfsm_reply(0); \
261		} }
262
263#define	nfsm_srvnamesiz(s) \
264		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
265		if (((s) = fxdr_unsigned(long,*tl)) > NFS_MAXNAMLEN) \
266			error = NFSERR_NAMETOL; \
267		if ((s) <= 0) \
268			error = EBADRPC; \
269		if (error) \
270			nfsm_reply(0); \
271		}
272
273#define nfsm_mtouio(p,s) \
274		if ((s) > 0 && \
275		   (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
276			error = t1; \
277			m_freem(mrep); \
278			goto nfsmout; \
279		}
280
281#define nfsm_uiotom(p,s) \
282		if (t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) { \
283			error = t1; \
284			m_freem(mreq); \
285			goto nfsmout; \
286		}
287
288#define	nfsm_reqhead(v,a,s) \
289		mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
290
291#define nfsm_reqdone	m_freem(mrep); \
292		nfsmout:
293
294#define nfsm_rndup(a)	(((a)+3)&(~0x3))
295
296#define	nfsm_request(v, t, p, c)	\
297		if (error = nfs_request((v), mreq, (t), (p), \
298		   (c), &mrep, &md, &dpos)) { \
299			if (error & NFSERR_RETERR) \
300				error &= ~NFSERR_RETERR; \
301			else \
302				goto nfsmout; \
303		}
304
305#define	nfsm_strtom(a,s,m) \
306		if ((s) > (m)) { \
307			m_freem(mreq); \
308			error = ENAMETOOLONG; \
309			goto nfsmout; \
310		} \
311		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
312		if (t2 <= M_TRAILINGSPACE(mb)) { \
313			nfsm_build(tl,u_long *,t2); \
314			*tl++ = txdr_unsigned(s); \
315			*(tl+((t2>>2)-2)) = 0; \
316			bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
317		} else if (t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) { \
318			error = t2; \
319			m_freem(mreq); \
320			goto nfsmout; \
321		}
322
323#define	nfsm_srvdone \
324		nfsmout: \
325		return(error)
326
327#define	nfsm_reply(s) \
328		{ \
329		nfsd->nd_repstat = error; \
330		if (error && !(nfsd->nd_flag & ND_NFSV3)) \
331		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
332			mrq, &mb, &bpos); \
333		else \
334		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
335			mrq, &mb, &bpos); \
336		m_freem(mrep); \
337		mreq = *mrq; \
338		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
339			error == EBADRPC)) \
340			return(0); \
341		}
342
343#define	nfsm_writereply(s, v3) \
344		{ \
345		nfsd->nd_repstat = error; \
346		if (error && !(v3)) \
347		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
348			&mreq, &mb, &bpos); \
349		else \
350		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
351			&mreq, &mb, &bpos); \
352		}
353
354#define	nfsm_adv(s) \
355		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
356		if (t1 >= (s)) { \
357			dpos += (s); \
358		} else if (t1 = nfs_adv(&md, &dpos, (s), t1)) { \
359			error = t1; \
360			m_freem(mrep); \
361			goto nfsmout; \
362		} }
363
364#define nfsm_srvmtofh(f) \
365		{ if (nfsd->nd_flag & ND_NFSV3) { \
366			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
367			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
368				error = EBADRPC; \
369				nfsm_reply(0); \
370			} \
371		} \
372		nfsm_dissect(tl, u_long *, NFSX_V3FH); \
373		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
374		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
375			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
376		}
377
378#define	nfsm_clget \
379		if (bp >= be) { \
380			if (mp == mb) \
381				mp->m_len += bp-bpos; \
382			MGET(mp, M_WAIT, MT_DATA); \
383			MCLGET(mp, M_WAIT); \
384			mp->m_len = NFSMSIZ(mp); \
385			mp2->m_next = mp; \
386			mp2 = mp; \
387			bp = mtod(mp, caddr_t); \
388			be = bp+mp->m_len; \
389		} \
390		tl = (u_long *)bp
391
392#define	nfsm_srvfillattr(a, f) \
393		nfsm_srvfattr(nfsd, (a), (f))
394
395#define nfsm_srvwcc_data(br, b, ar, a) \
396		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
397
398#define nfsm_srvpostop_attr(r, a) \
399		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
400
401#define nfsm_srvsattr(a) \
402		{ nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
403		if (*tl == nfs_true) { \
404			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
405			(a)->va_mode = nfstov_mode(*tl); \
406		} \
407		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
408		if (*tl == nfs_true) { \
409			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
410			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
411		} \
412		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
413		if (*tl == nfs_true) { \
414			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
415			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
416		} \
417		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
418		if (*tl == nfs_true) { \
419			nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
420			fxdr_hyper(tl, &(a)->va_size); \
421		} \
422		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
423		switch (fxdr_unsigned(int, *tl)) { \
424		case NFSV3SATTRTIME_TOCLIENT: \
425			nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
426			fxdr_nfsv3time(tl, &(a)->va_atime); \
427			break; \
428		case NFSV3SATTRTIME_TOSERVER: \
429			(a)->va_atime.tv_sec = time.tv_sec; \
430			(a)->va_atime.tv_nsec = time.tv_usec * 1000; \
431			break; \
432		}; \
433		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
434		switch (fxdr_unsigned(int, *tl)) { \
435		case NFSV3SATTRTIME_TOCLIENT: \
436			nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
437			fxdr_nfsv3time(tl, &(a)->va_mtime); \
438			break; \
439		case NFSV3SATTRTIME_TOSERVER: \
440			(a)->va_mtime.tv_sec = time.tv_sec; \
441			(a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
442			break; \
443		}; }
444
445#endif
446