/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1989, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Rick Macklem at The University of Guelph.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#ifndef _NFS_NFSM_SUBS_H_35#define _NFS_NFSM_SUBS_H_3637/*38* These macros do strange and peculiar things to mbuf chains for39* the assistance of the nfs code. To attempt to use them for any40* other purpose will be dangerous. (they make weird assumptions)41*/4243/*44* First define what the actual subs. return45*/46#define NFSM_DATAP(m, s) (m)->m_data += (s)4748/*49* Now for the macros that do the simple stuff and call the functions50* for the hard stuff.51* They use fields in struct nfsrv_descript to handle the mbuf queues.52* Replace most of the macro with an inline function, to minimize53* the machine code. The inline functions in lower case can be called54* directly, bypassing the macro.55*/56static __inline void *57nfsm_build(struct nfsrv_descript *nd, int siz)58{59void *retp;60struct mbuf *mb2;6162if ((nd->nd_flag & ND_EXTPG) == 0 &&63siz > M_TRAILINGSPACE(nd->nd_mb)) {64NFSMCLGET(mb2, M_NOWAIT);65if (siz > MLEN)66panic("build > MLEN");67mb2->m_len = 0;68nd->nd_bpos = mtod(mb2, char *);69nd->nd_mb->m_next = mb2;70nd->nd_mb = mb2;71} else if ((nd->nd_flag & ND_EXTPG) != 0) {72if (siz > nd->nd_bextpgsiz) {73mb2 = mb_alloc_ext_plus_pages(PAGE_SIZE, M_WAITOK);74nd->nd_bpos = (char *)(void *)75PHYS_TO_DMAP(mb2->m_epg_pa[0]);76nd->nd_bextpg = 0;77nd->nd_bextpgsiz = PAGE_SIZE - siz;78nd->nd_mb->m_next = mb2;79nd->nd_mb = mb2;80} else81nd->nd_bextpgsiz -= siz;82nd->nd_mb->m_epg_last_len += siz;83}84retp = (void *)(nd->nd_bpos);85nd->nd_mb->m_len += siz;86nd->nd_bpos += siz;87return (retp);88}8990#define NFSM_BUILD(a, c, s) ((a) = (c)nfsm_build(nd, (s)))9192static __inline void *93nfsm_dissect(struct nfsrv_descript *nd, int siz)94{95int tt1;96void *retp;9798tt1 = mtod(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;99if (tt1 >= siz) {100retp = (void *)nd->nd_dpos;101nd->nd_dpos += siz;102} else {103retp = nfsm_dissct(nd, siz, M_WAITOK);104}105return (retp);106}107108static __inline void *109nfsm_dissect_nonblock(struct nfsrv_descript *nd, int siz)110{111int tt1;112void *retp;113114tt1 = mtod(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;115if (tt1 >= siz) {116retp = (void *)nd->nd_dpos;117nd->nd_dpos += siz;118} else {119retp = nfsm_dissct(nd, siz, M_NOWAIT);120}121return (retp);122}123124#define NFSM_DISSECT(a, c, s) \125do { \126(a) = (c)nfsm_dissect(nd, (s)); \127if ((a) == NULL) { \128error = EBADRPC; \129goto nfsmout; \130} \131} while (0)132133#define NFSM_DISSECT_NONBLOCK(a, c, s) \134do { \135(a) = (c)nfsm_dissect_nonblock(nd, (s)); \136if ((a) == NULL) { \137error = EBADRPC; \138goto nfsmout; \139} \140} while (0)141142#define NFSM_STRSIZ(s, m) \143do { \144tl = (u_int32_t *)nfsm_dissect(nd, NFSX_UNSIGNED); \145if (!tl || ((s) = fxdr_unsigned(int32_t, *tl)) > (m)) { \146error = EBADRPC; \147goto nfsmout; \148} \149} while (0)150151#define NFSM_RNDUP(a) (((a)+3)&(~0x3))152153#endif /* _NFS_NFSM_SUBS_H_ */154155156