nfsm_subs.h revision 9336
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.6 1995/05/30 08:12:51 rgrimes 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 */
53extern struct mbuf *nfsm_reqh();
54
55#define	M_HASCL(m)	((m)->m_flags & M_EXT)
56#define	NFSMINOFF(m) \
57		if (M_HASCL(m)) \
58			(m)->m_data = (m)->m_ext.ext_buf; \
59		else if ((m)->m_flags & M_PKTHDR) \
60			(m)->m_data = (m)->m_pktdat; \
61		else \
62			(m)->m_data = (m)->m_dat
63#define	NFSMADV(m, s)	(m)->m_data += (s)
64#define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: \
65				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
66
67/*
68 * Now for the macros that do the simple stuff and call the functions
69 * for the hard stuff.
70 * These macros use several vars. declared in nfsm_reqhead and these
71 * vars. must not be used elsewhere unless you are careful not to corrupt
72 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
73 * that may be used so long as the value is not expected to retained
74 * after a macro.
75 * I know, this is kind of dorkey, but it makes the actual op functions
76 * fairly clean and deals with the mess caused by the xdr discriminating
77 * unions.
78 */
79
80#define	nfsm_build(a,c,s) \
81		{ if ((s) > M_TRAILINGSPACE(mb)) { \
82			MGET(mb2, M_WAIT, MT_DATA); \
83			if ((s) > MLEN) \
84				panic("build > MLEN"); \
85			mb->m_next = mb2; \
86			mb = mb2; \
87			mb->m_len = 0; \
88			bpos = mtod(mb, caddr_t); \
89		} \
90		(a) = (c)(bpos); \
91		mb->m_len += (s); \
92		bpos += (s); }
93
94#define	nfsm_dissect(a, c, s) \
95		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
96		if (t1 >= (s)) { \
97			(a) = (c)(dpos); \
98			dpos += (s); \
99		} else if (t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) { \
100			error = t1; \
101			m_freem(mrep); \
102			goto nfsmout; \
103		} else { \
104			(a) = (c)cp2; \
105		} }
106
107#define nfsm_fhtom(v, v3) \
108	      { if (v3) { \
109			t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
110			if (t2 <= M_TRAILINGSPACE(mb)) { \
111				nfsm_build(tl, u_long *, t2); \
112				*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
113				*(tl + ((t2>>2) - 2)) = 0; \
114				bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
115					VTONFS(v)->n_fhsize); \
116			} else if (t2 = nfsm_strtmbuf(&mb, &bpos, \
117				(caddr_t)VTONFS(v)->n_fhp, VTONFS(v)->n_fhsize)) { \
118				error = t2; \
119				m_freem(mreq); \
120				goto nfsmout; \
121			} \
122		} else { \
123			nfsm_build(cp, caddr_t, NFSX_V2FH); \
124			bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
125		} }
126
127#define nfsm_srvfhtom(f, v3) \
128		{ if (v3) { \
129			nfsm_build(tl, u_long *, NFSX_UNSIGNED + NFSX_V3FH); \
130			*tl++ = txdr_unsigned(NFSX_V3FH); \
131			bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
132		} else { \
133			nfsm_build(cp, caddr_t, NFSX_V2FH); \
134			bcopy((caddr_t)(f), cp, NFSX_V2FH); \
135		} }
136
137#define nfsm_srvpostop_fh(f) \
138		{ nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
139		*tl++ = nfs_true; \
140		*tl++ = txdr_unsigned(NFSX_V3FH); \
141		bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
142		}
143
144#define nfsm_mtofh(d, v, v3, f) \
145		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
146		if (v3) { \
147			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
148			(f) = fxdr_unsigned(int, *tl); \
149		} else \
150			(f) = 1; \
151		if (f) { \
152			nfsm_getfh(ttfhp, ttfhsize, (v3)); \
153			if (t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
154				&ttnp)) { \
155				error = t1; \
156				m_freem(mrep); \
157				goto nfsmout; \
158			} \
159			(v) = NFSTOV(ttnp); \
160		} \
161		if (v3) { \
162			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
163			if (f) \
164				(f) = fxdr_unsigned(int, *tl); \
165			else if (fxdr_unsigned(int, *tl)) \
166				nfsm_adv(NFSX_V3FATTR); \
167		} \
168		if (f) \
169			nfsm_loadattr((v), (struct vattr *)0); \
170		}
171
172#define nfsm_getfh(f, s, v3) \
173		{ if (v3) { \
174			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
175			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
176				(s) > NFSX_V3FHMAX) { \
177				m_freem(mrep); \
178				error = EBADRPC; \
179				goto nfsmout; \
180			} \
181		} else \
182			(s) = NFSX_V2FH; \
183		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
184
185#define	nfsm_loadattr(v, a) \
186		{ struct vnode *ttvp = (v); \
187		if (t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a))) { \
188			error = t1; \
189			m_freem(mrep); \
190			goto nfsmout; \
191		} \
192		(v) = ttvp; }
193
194#define	nfsm_postop_attr(v, f) \
195		{ struct vnode *ttvp = (v); \
196		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
197		if ((f) = fxdr_unsigned(int, *tl)) { \
198			if (t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
199				(struct vattr *)0)) { \
200				error = t1; \
201				(f) = 0; \
202				m_freem(mrep); \
203				goto nfsmout; \
204			} \
205			(v) = ttvp; \
206		} }
207
208/* Used as (f) for nfsm_wcc_data() */
209#define NFSV3_WCCRATTR	0
210#define NFSV3_WCCCHK	1
211
212#define	nfsm_wcc_data(v, f) \
213		{ int ttattrf, ttretf = 0; \
214		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
215		if (*tl == nfs_true) { \
216			nfsm_dissect(tl, u_long *, 6 * NFSX_UNSIGNED); \
217			if (f) \
218				ttretf = (VTONFS(v)->n_mtime == \
219					fxdr_unsigned(u_long, *(tl + 2))); \
220		} \
221		nfsm_postop_attr((v), ttattrf); \
222		if (f) { \
223			(f) = ttretf; \
224		} else { \
225			(f) = ttattrf; \
226		} }
227
228#define nfsm_v3sattr(s, a, u, g) \
229		{ (s)->sa_modetrue = nfs_true; \
230		(s)->sa_mode = vtonfsv3_mode((a)->va_mode); \
231		(s)->sa_uidtrue = nfs_true; \
232		(s)->sa_uid = txdr_unsigned(u); \
233		(s)->sa_gidtrue = nfs_true; \
234		(s)->sa_gid = txdr_unsigned(g); \
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_long *,NFSX_UNSIGNED); \
244		if (((s) = fxdr_unsigned(long,*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_long *,NFSX_UNSIGNED); \
252		if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
253			error = EBADRPC; \
254			nfsm_reply(0); \
255		} }
256
257#define	nfsm_srvnamesiz(s) \
258		{ nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
259		if (((s) = fxdr_unsigned(long,*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))) { \
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)) { \
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)) { \
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_long *,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))) { \
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		m_freem(mrep); \
331		mreq = *mrq; \
332		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
333			error == EBADRPC)) \
334			return(0); \
335		}
336
337#define	nfsm_writereply(s, v3) \
338		{ \
339		nfsd->nd_repstat = error; \
340		if (error && !(v3)) \
341		   (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
342			&mreq, &mb, &bpos); \
343		else \
344		   (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
345			&mreq, &mb, &bpos); \
346		}
347
348#define	nfsm_adv(s) \
349		{ t1 = mtod(md, caddr_t)+md->m_len-dpos; \
350		if (t1 >= (s)) { \
351			dpos += (s); \
352		} else if (t1 = nfs_adv(&md, &dpos, (s), t1)) { \
353			error = t1; \
354			m_freem(mrep); \
355			goto nfsmout; \
356		} }
357
358#define nfsm_srvmtofh(f) \
359		{ if (nfsd->nd_flag & ND_NFSV3) { \
360			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
361			if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
362				error = EBADRPC; \
363				nfsm_reply(0); \
364			} \
365		} \
366		nfsm_dissect(tl, u_long *, NFSX_V3FH); \
367		bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
368		if ((nfsd->nd_flag & ND_NFSV3) == 0) \
369			nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
370		}
371
372#define	nfsm_clget \
373		if (bp >= be) { \
374			if (mp == mb) \
375				mp->m_len += bp-bpos; \
376			MGET(mp, M_WAIT, MT_DATA); \
377			MCLGET(mp, M_WAIT); \
378			mp->m_len = NFSMSIZ(mp); \
379			mp2->m_next = mp; \
380			mp2 = mp; \
381			bp = mtod(mp, caddr_t); \
382			be = bp+mp->m_len; \
383		} \
384		tl = (u_long *)bp
385
386#define	nfsm_srvfillattr(a, f) \
387		nfsm_srvfattr(nfsd, (a), (f))
388
389#define nfsm_srvwcc_data(br, b, ar, a) \
390		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
391
392#define nfsm_srvpostop_attr(r, a) \
393		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
394
395#define nfsm_srvsattr(a) \
396		{ nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
397		if (*tl == nfs_true) { \
398			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
399			(a)->va_mode = nfstov_mode(*tl); \
400		} \
401		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
402		if (*tl == nfs_true) { \
403			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
404			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \
405		} \
406		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
407		if (*tl == nfs_true) { \
408			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
409			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \
410		} \
411		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
412		if (*tl == nfs_true) { \
413			nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
414			fxdr_hyper(tl, &(a)->va_size); \
415		} \
416		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
417		switch (fxdr_unsigned(int, *tl)) { \
418		case NFSV3SATTRTIME_TOCLIENT: \
419			nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
420			fxdr_nfsv3time(tl, &(a)->va_atime); \
421			break; \
422		case NFSV3SATTRTIME_TOSERVER: \
423			(a)->va_atime.ts_sec = time.tv_sec; \
424			(a)->va_atime.ts_nsec = time.tv_usec * 1000; \
425			break; \
426		}; \
427		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
428		switch (fxdr_unsigned(int, *tl)) { \
429		case NFSV3SATTRTIME_TOCLIENT: \
430			nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
431			fxdr_nfsv3time(tl, &(a)->va_mtime); \
432			break; \
433		case NFSV3SATTRTIME_TOSERVER: \
434			(a)->va_mtime.ts_sec = time.tv_sec; \
435			(a)->va_mtime.ts_nsec = time.tv_usec * 1000; \
436			break; \
437		}; }
438
439#endif
440