xref: /freebsd/sys/fs/nfs/nfsm_subs.h (revision e17f5b1d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $FreeBSD$
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	NFSM_DATAP(m, s)	(m)->m_data += (s)
52 
53 /*
54  * Now for the macros that do the simple stuff and call the functions
55  * for the hard stuff.
56  * They use fields in struct nfsrv_descript to handle the mbuf queues.
57  * Replace most of the macro with an inline function, to minimize
58  * the machine code. The inline functions in lower case can be called
59  * directly, bypassing the macro.
60  */
61 static __inline void *
62 nfsm_build(struct nfsrv_descript *nd, int siz)
63 {
64 	void *retp;
65 	struct mbuf *mb2;
66 
67 	if ((nd->nd_flag & ND_EXTPG) == 0 &&
68 	    siz > M_TRAILINGSPACE(nd->nd_mb)) {
69 		NFSMCLGET(mb2, M_NOWAIT);
70 		if (siz > MLEN)
71 			panic("build > MLEN");
72 		mb2->m_len = 0;
73 		nd->nd_bpos = mtod(mb2, char *);
74 		nd->nd_mb->m_next = mb2;
75 		nd->nd_mb = mb2;
76 	} else if ((nd->nd_flag & ND_EXTPG) != 0) {
77 		if (siz > nd->nd_bextpgsiz) {
78 			mb2 = mb_alloc_ext_plus_pages(PAGE_SIZE, M_WAITOK);
79 			nd->nd_bpos = (char *)(void *)
80 			    PHYS_TO_DMAP(mb2->m_epg_pa[0]);
81 			nd->nd_bextpg = 0;
82 			nd->nd_bextpgsiz = PAGE_SIZE - siz;
83 			nd->nd_mb->m_next = mb2;
84 			nd->nd_mb = mb2;
85 		} else
86 			nd->nd_bextpgsiz -= siz;
87 		nd->nd_mb->m_epg_last_len += siz;
88 	}
89 	retp = (void *)(nd->nd_bpos);
90 	nd->nd_mb->m_len += siz;
91 	nd->nd_bpos += siz;
92 	return (retp);
93 }
94 
95 #define	NFSM_BUILD(a, c, s)	((a) = (c)nfsm_build(nd, (s)))
96 
97 static __inline void *
98 nfsm_dissect(struct nfsrv_descript *nd, int siz)
99 {
100 	int tt1;
101 	void *retp;
102 
103 	tt1 = mtod(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;
104 	if (tt1 >= siz) {
105 		retp = (void *)nd->nd_dpos;
106 		nd->nd_dpos += siz;
107 	} else {
108 		retp = nfsm_dissct(nd, siz, M_WAITOK);
109 	}
110 	return (retp);
111 }
112 
113 static __inline void *
114 nfsm_dissect_nonblock(struct nfsrv_descript *nd, int siz)
115 {
116 	int tt1;
117 	void *retp;
118 
119 	tt1 = mtod(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;
120 	if (tt1 >= siz) {
121 		retp = (void *)nd->nd_dpos;
122 		nd->nd_dpos += siz;
123 	} else {
124 		retp = nfsm_dissct(nd, siz, M_NOWAIT);
125 	}
126 	return (retp);
127 }
128 
129 #define	NFSM_DISSECT(a, c, s) 						\
130 	do {								\
131 		(a) = (c)nfsm_dissect(nd, (s));	 			\
132 		if ((a) == NULL) { 					\
133 			error = EBADRPC; 				\
134 			goto nfsmout; 					\
135 		}							\
136 	} while (0)
137 
138 #define	NFSM_DISSECT_NONBLOCK(a, c, s) 					\
139 	do {								\
140 		(a) = (c)nfsm_dissect_nonblock(nd, (s));		\
141 		if ((a) == NULL) { 					\
142 			error = EBADRPC; 				\
143 			goto nfsmout; 					\
144 		}							\
145 	} while (0)
146 #endif	/* !APPLE */
147 
148 #define	NFSM_STRSIZ(s, m)  						\
149 	do {								\
150 		tl = (u_int32_t *)nfsm_dissect(nd, NFSX_UNSIGNED);	\
151 		if (!tl || ((s) = fxdr_unsigned(int32_t, *tl)) > (m)) { \
152 			error = EBADRPC; 				\
153 			goto nfsmout; 					\
154 		}							\
155 	} while (0)
156 
157 #define	NFSM_RNDUP(a)	(((a)+3)&(~0x3))
158 
159 #endif	/* _NFS_NFSM_SUBS_H_ */
160