nfsm_subs.h revision 1.2
1/*	$NetBSD: nfsm_subs.h,v 1.2 2016/07/07 06:55:42 msaitoh Exp $	*/
2/*-
3 * Copyright (c) 1989, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Rick Macklem at The University of Guelph.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * FreeBSD: head/sys/fs/nfs/nfsm_subs.h 249592 2013-04-17 21:00:22Z ken
34 * $NetBSD: nfsm_subs.h,v 1.2 2016/07/07 06:55:42 msaitoh Exp $
35 */
36
37#ifndef _NFS_NFSM_SUBS_H_
38#define	_NFS_NFSM_SUBS_H_
39
40
41/*
42 * These macros do strange and peculiar things to mbuf chains for
43 * the assistance of the nfs code. To attempt to use them for any
44 * other purpose will be dangerous. (they make weird assumptions)
45 */
46
47#ifndef APPLE
48/*
49 * First define what the actual subs. return
50 */
51#define	M_HASCL(m)	((m)->m_flags & M_EXT)
52#define	NFSMINOFF(m) 							\
53		if (M_HASCL(m)) 					\
54			(m)->m_data = (m)->m_ext.ext_buf; 		\
55		else if ((m)->m_flags & M_PKTHDR) 			\
56			(m)->m_data = (m)->m_pktdat; 			\
57				else 					\
58			(m)->m_data = (m)->m_dat
59#define	NFSMSIZ(m)	((M_HASCL(m))?MCLBYTES: 			\
60				(((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
61#define	NFSM_DATAP(m, s)	(m)->m_data += (s)
62
63/*
64 * Now for the macros that do the simple stuff and call the functions
65 * for the hard stuff.
66 * They use fields in struct nfsrv_descript to handle the mbuf queues.
67 * Replace most of the macro with an inline function, to minimize
68 * the machine code. The inline functions in lower case can be called
69 * directly, bypassing the macro.
70 */
71static __inline void *
72nfsm_build(struct nfsrv_descript *nd, int siz)
73{
74	void *retp;
75	struct mbuf *mb2;
76
77	if (siz > M_TRAILINGSPACE(nd->nd_mb)) {
78		NFSMCLGET(mb2, M_NOWAIT);
79		if (siz > MLEN)
80			panic("build > MLEN");
81		mbuf_setlen(mb2, 0);
82		nd->nd_bpos = NFSMTOD(mb2, caddr_t);
83		nd->nd_mb->m_next = mb2;
84		nd->nd_mb = mb2;
85	}
86	retp = (void *)(nd->nd_bpos);
87	nd->nd_mb->m_len += siz;
88	nd->nd_bpos += siz;
89	return (retp);
90}
91
92#define	NFSM_BUILD(a, c, s)	((a) = (c)nfsm_build(nd, (s)))
93
94static __inline void *
95nfsm_dissect(struct nfsrv_descript *nd, int siz)
96{
97	int tt1;
98	void *retp;
99
100	tt1 = NFSMTOD(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;
101	if (tt1 >= siz) {
102		retp = (void *)nd->nd_dpos;
103		nd->nd_dpos += siz;
104	} else {
105		retp = nfsm_dissct(nd, siz, M_WAITOK);
106	}
107	return (retp);
108}
109
110static __inline void *
111nfsm_dissect_nonblock(struct nfsrv_descript *nd, int siz)
112{
113	int tt1;
114	void *retp;
115
116	tt1 = NFSMTOD(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;
117	if (tt1 >= siz) {
118		retp = (void *)nd->nd_dpos;
119		nd->nd_dpos += siz;
120	} else {
121		retp = nfsm_dissct(nd, siz, M_NOWAIT);
122	}
123	return (retp);
124}
125
126#define	NFSM_DISSECT(a, c, s) 						\
127	do {								\
128		(a) = (c)nfsm_dissect(nd, (s));	 			\
129		if ((a) == NULL) { 					\
130			error = EBADRPC; 				\
131			goto nfsmout; 					\
132		}							\
133	} while (0)
134
135#define	NFSM_DISSECT_NONBLOCK(a, c, s) 					\
136	do {								\
137		(a) = (c)nfsm_dissect_nonblock(nd, (s));		\
138		if ((a) == NULL) { 					\
139			error = EBADRPC; 				\
140			goto nfsmout; 					\
141		}							\
142	} while (0)
143#endif	/* !APPLE */
144
145#define	NFSM_STRSIZ(s, m)  						\
146	do {								\
147		tl = (u_int32_t *)nfsm_dissect(nd, NFSX_UNSIGNED);	\
148		if (!tl || ((s) = fxdr_unsigned(int32_t, *tl)) > (m)) { \
149			error = EBADRPC; 				\
150			goto nfsmout; 					\
151		}							\
152	} while (0)
153
154#define	NFSM_RNDUP(a)	(((a)+3)&(~0x3))
155
156#endif	/* _NFS_NFSM_SUBS_H_ */
157