19ec7b004SRick Macklem /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
49ec7b004SRick Macklem * Copyright (c) 1989, 1993
59ec7b004SRick Macklem * The Regents of the University of California. All rights reserved.
69ec7b004SRick Macklem *
79ec7b004SRick Macklem * This code is derived from software contributed to Berkeley by
89ec7b004SRick Macklem * Rick Macklem at The University of Guelph.
99ec7b004SRick Macklem *
109ec7b004SRick Macklem * Redistribution and use in source and binary forms, with or without
119ec7b004SRick Macklem * modification, are permitted provided that the following conditions
129ec7b004SRick Macklem * are met:
139ec7b004SRick Macklem * 1. Redistributions of source code must retain the above copyright
149ec7b004SRick Macklem * notice, this list of conditions and the following disclaimer.
159ec7b004SRick Macklem * 2. Redistributions in binary form must reproduce the above copyright
169ec7b004SRick Macklem * notice, this list of conditions and the following disclaimer in the
179ec7b004SRick Macklem * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
199ec7b004SRick Macklem * may be used to endorse or promote products derived from this software
209ec7b004SRick Macklem * without specific prior written permission.
219ec7b004SRick Macklem *
229ec7b004SRick Macklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239ec7b004SRick Macklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249ec7b004SRick Macklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259ec7b004SRick Macklem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269ec7b004SRick Macklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279ec7b004SRick Macklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289ec7b004SRick Macklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299ec7b004SRick Macklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309ec7b004SRick Macklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319ec7b004SRick Macklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329ec7b004SRick Macklem * SUCH DAMAGE.
339ec7b004SRick Macklem */
349ec7b004SRick Macklem
359ec7b004SRick Macklem #ifndef _NFS_NFSM_SUBS_H_
369ec7b004SRick Macklem #define _NFS_NFSM_SUBS_H_
379ec7b004SRick Macklem
389ec7b004SRick Macklem /*
399ec7b004SRick Macklem * These macros do strange and peculiar things to mbuf chains for
409ec7b004SRick Macklem * the assistance of the nfs code. To attempt to use them for any
419ec7b004SRick Macklem * other purpose will be dangerous. (they make weird assumptions)
429ec7b004SRick Macklem */
439ec7b004SRick Macklem
449ec7b004SRick Macklem /*
459ec7b004SRick Macklem * First define what the actual subs. return
469ec7b004SRick Macklem */
479ec7b004SRick Macklem #define NFSM_DATAP(m, s) (m)->m_data += (s)
489ec7b004SRick Macklem
499ec7b004SRick Macklem /*
509ec7b004SRick Macklem * Now for the macros that do the simple stuff and call the functions
519ec7b004SRick Macklem * for the hard stuff.
529ec7b004SRick Macklem * They use fields in struct nfsrv_descript to handle the mbuf queues.
539ec7b004SRick Macklem * Replace most of the macro with an inline function, to minimize
549ec7b004SRick Macklem * the machine code. The inline functions in lower case can be called
559ec7b004SRick Macklem * directly, bypassing the macro.
569ec7b004SRick Macklem */
579ec7b004SRick Macklem static __inline void *
nfsm_build(struct nfsrv_descript * nd,int siz)589ec7b004SRick Macklem nfsm_build(struct nfsrv_descript *nd, int siz)
599ec7b004SRick Macklem {
609ec7b004SRick Macklem void *retp;
619ec7b004SRick Macklem struct mbuf *mb2;
629ec7b004SRick Macklem
63*2da15278SRick Macklem if ((nd->nd_flag & ND_EXTPG) == 0 &&
64*2da15278SRick Macklem siz > M_TRAILINGSPACE(nd->nd_mb)) {
65eb1b1807SGleb Smirnoff NFSMCLGET(mb2, M_NOWAIT);
669ec7b004SRick Macklem if (siz > MLEN)
679ec7b004SRick Macklem panic("build > MLEN");
689f6624d3SRick Macklem mb2->m_len = 0;
69*2da15278SRick Macklem nd->nd_bpos = mtod(mb2, char *);
709ec7b004SRick Macklem nd->nd_mb->m_next = mb2;
719ec7b004SRick Macklem nd->nd_mb = mb2;
72*2da15278SRick Macklem } else if ((nd->nd_flag & ND_EXTPG) != 0) {
73*2da15278SRick Macklem if (siz > nd->nd_bextpgsiz) {
74*2da15278SRick Macklem mb2 = mb_alloc_ext_plus_pages(PAGE_SIZE, M_WAITOK);
75*2da15278SRick Macklem nd->nd_bpos = (char *)(void *)
76*2da15278SRick Macklem PHYS_TO_DMAP(mb2->m_epg_pa[0]);
77*2da15278SRick Macklem nd->nd_bextpg = 0;
78*2da15278SRick Macklem nd->nd_bextpgsiz = PAGE_SIZE - siz;
79*2da15278SRick Macklem nd->nd_mb->m_next = mb2;
80*2da15278SRick Macklem nd->nd_mb = mb2;
81*2da15278SRick Macklem } else
82*2da15278SRick Macklem nd->nd_bextpgsiz -= siz;
83*2da15278SRick Macklem nd->nd_mb->m_epg_last_len += siz;
849ec7b004SRick Macklem }
859ec7b004SRick Macklem retp = (void *)(nd->nd_bpos);
869ec7b004SRick Macklem nd->nd_mb->m_len += siz;
879ec7b004SRick Macklem nd->nd_bpos += siz;
889ec7b004SRick Macklem return (retp);
899ec7b004SRick Macklem }
909ec7b004SRick Macklem
919ec7b004SRick Macklem #define NFSM_BUILD(a, c, s) ((a) = (c)nfsm_build(nd, (s)))
929ec7b004SRick Macklem
939ec7b004SRick Macklem static __inline void *
nfsm_dissect(struct nfsrv_descript * nd,int siz)949ec7b004SRick Macklem nfsm_dissect(struct nfsrv_descript *nd, int siz)
959ec7b004SRick Macklem {
969ec7b004SRick Macklem int tt1;
979ec7b004SRick Macklem void *retp;
989ec7b004SRick Macklem
999f6624d3SRick Macklem tt1 = mtod(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;
1009ec7b004SRick Macklem if (tt1 >= siz) {
1019ec7b004SRick Macklem retp = (void *)nd->nd_dpos;
1029ec7b004SRick Macklem nd->nd_dpos += siz;
1039ec7b004SRick Macklem } else {
104d96b98a3SKenneth D. Merry retp = nfsm_dissct(nd, siz, M_WAITOK);
105d96b98a3SKenneth D. Merry }
106d96b98a3SKenneth D. Merry return (retp);
107d96b98a3SKenneth D. Merry }
108d96b98a3SKenneth D. Merry
109d96b98a3SKenneth D. Merry static __inline void *
nfsm_dissect_nonblock(struct nfsrv_descript * nd,int siz)110d96b98a3SKenneth D. Merry nfsm_dissect_nonblock(struct nfsrv_descript *nd, int siz)
111d96b98a3SKenneth D. Merry {
112d96b98a3SKenneth D. Merry int tt1;
113d96b98a3SKenneth D. Merry void *retp;
114d96b98a3SKenneth D. Merry
1159f6624d3SRick Macklem tt1 = mtod(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;
116d96b98a3SKenneth D. Merry if (tt1 >= siz) {
117d96b98a3SKenneth D. Merry retp = (void *)nd->nd_dpos;
118d96b98a3SKenneth D. Merry nd->nd_dpos += siz;
119d96b98a3SKenneth D. Merry } else {
120d96b98a3SKenneth D. Merry retp = nfsm_dissct(nd, siz, M_NOWAIT);
1219ec7b004SRick Macklem }
1229ec7b004SRick Macklem return (retp);
1239ec7b004SRick Macklem }
1249ec7b004SRick Macklem
1259ec7b004SRick Macklem #define NFSM_DISSECT(a, c, s) \
1269ec7b004SRick Macklem do { \
1279ec7b004SRick Macklem (a) = (c)nfsm_dissect(nd, (s)); \
1289ec7b004SRick Macklem if ((a) == NULL) { \
1299ec7b004SRick Macklem error = EBADRPC; \
1309ec7b004SRick Macklem goto nfsmout; \
1319ec7b004SRick Macklem } \
1329ec7b004SRick Macklem } while (0)
133d96b98a3SKenneth D. Merry
134d96b98a3SKenneth D. Merry #define NFSM_DISSECT_NONBLOCK(a, c, s) \
135d96b98a3SKenneth D. Merry do { \
136d96b98a3SKenneth D. Merry (a) = (c)nfsm_dissect_nonblock(nd, (s)); \
137d96b98a3SKenneth D. Merry if ((a) == NULL) { \
138d96b98a3SKenneth D. Merry error = EBADRPC; \
139d96b98a3SKenneth D. Merry goto nfsmout; \
140d96b98a3SKenneth D. Merry } \
141d96b98a3SKenneth D. Merry } while (0)
1429ec7b004SRick Macklem
1439ec7b004SRick Macklem #define NFSM_STRSIZ(s, m) \
1449ec7b004SRick Macklem do { \
1459ec7b004SRick Macklem tl = (u_int32_t *)nfsm_dissect(nd, NFSX_UNSIGNED); \
1469ec7b004SRick Macklem if (!tl || ((s) = fxdr_unsigned(int32_t, *tl)) > (m)) { \
1479ec7b004SRick Macklem error = EBADRPC; \
1489ec7b004SRick Macklem goto nfsmout; \
1499ec7b004SRick Macklem } \
1509ec7b004SRick Macklem } while (0)
1519ec7b004SRick Macklem
1529ec7b004SRick Macklem #define NFSM_RNDUP(a) (((a)+3)&(~0x3))
1539ec7b004SRick Macklem
1549ec7b004SRick Macklem #endif /* _NFS_NFSM_SUBS_H_ */
155