xref: /netbsd-src/sys/fs/nfs/common/nfsm_subs.h (revision 0342d5080b4aa2e64ad6d1da61f9e3e072466754)
1*0342d508Spgoyette /*	$NetBSD: nfsm_subs.h,v 1.4 2016/12/13 22:52:46 pgoyette Exp $	*/
26ca35587Sdholland /*-
36ca35587Sdholland  * Copyright (c) 1989, 1993
46ca35587Sdholland  *	The Regents of the University of California.  All rights reserved.
56ca35587Sdholland  *
66ca35587Sdholland  * This code is derived from software contributed to Berkeley by
76ca35587Sdholland  * Rick Macklem at The University of Guelph.
86ca35587Sdholland  *
96ca35587Sdholland  * Redistribution and use in source and binary forms, with or without
106ca35587Sdholland  * modification, are permitted provided that the following conditions
116ca35587Sdholland  * are met:
126ca35587Sdholland  * 1. Redistributions of source code must retain the above copyright
136ca35587Sdholland  *    notice, this list of conditions and the following disclaimer.
146ca35587Sdholland  * 2. Redistributions in binary form must reproduce the above copyright
156ca35587Sdholland  *    notice, this list of conditions and the following disclaimer in the
166ca35587Sdholland  *    documentation and/or other materials provided with the distribution.
176ca35587Sdholland  * 4. Neither the name of the University nor the names of its contributors
186ca35587Sdholland  *    may be used to endorse or promote products derived from this software
196ca35587Sdholland  *    without specific prior written permission.
206ca35587Sdholland  *
216ca35587Sdholland  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
226ca35587Sdholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
236ca35587Sdholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
246ca35587Sdholland  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
256ca35587Sdholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
266ca35587Sdholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
276ca35587Sdholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
286ca35587Sdholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
296ca35587Sdholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
306ca35587Sdholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
316ca35587Sdholland  * SUCH DAMAGE.
326ca35587Sdholland  *
332d39560cSpgoyette  * FreeBSD: head/sys/fs/nfs/nfsm_subs.h 276780 2015-01-07 17:22:56Z rwatson
34*0342d508Spgoyette  * $NetBSD: nfsm_subs.h,v 1.4 2016/12/13 22:52:46 pgoyette Exp $
356ca35587Sdholland  */
366ca35587Sdholland 
376ca35587Sdholland #ifndef _NFS_NFSM_SUBS_H_
386ca35587Sdholland #define	_NFS_NFSM_SUBS_H_
396ca35587Sdholland 
406ca35587Sdholland 
416ca35587Sdholland /*
426ca35587Sdholland  * These macros do strange and peculiar things to mbuf chains for
436ca35587Sdholland  * the assistance of the nfs code. To attempt to use them for any
446ca35587Sdholland  * other purpose will be dangerous. (they make weird assumptions)
456ca35587Sdholland  */
466ca35587Sdholland 
476ca35587Sdholland #ifndef APPLE
486ca35587Sdholland /*
496ca35587Sdholland  * First define what the actual subs. return
506ca35587Sdholland  */
516ca35587Sdholland #define	NFSM_DATAP(m, s)	(m)->m_data += (s)
526ca35587Sdholland 
536ca35587Sdholland /*
546ca35587Sdholland  * Now for the macros that do the simple stuff and call the functions
556ca35587Sdholland  * for the hard stuff.
566ca35587Sdholland  * They use fields in struct nfsrv_descript to handle the mbuf queues.
576ca35587Sdholland  * Replace most of the macro with an inline function, to minimize
586ca35587Sdholland  * the machine code. The inline functions in lower case can be called
596ca35587Sdholland  * directly, bypassing the macro.
606ca35587Sdholland  */
616ca35587Sdholland static __inline void *
nfsm_build(struct nfsrv_descript * nd,int siz)626ca35587Sdholland nfsm_build(struct nfsrv_descript *nd, int siz)
636ca35587Sdholland {
646ca35587Sdholland 	void *retp;
656ca35587Sdholland 	struct mbuf *mb2;
666ca35587Sdholland 
676ca35587Sdholland 	if (siz > M_TRAILINGSPACE(nd->nd_mb)) {
686ca35587Sdholland 		NFSMCLGET(mb2, M_NOWAIT);
696ca35587Sdholland 		if (siz > MLEN)
706ca35587Sdholland 			panic("build > MLEN");
716ca35587Sdholland 		mbuf_setlen(mb2, 0);
72*0342d508Spgoyette 		nd->nd_bpos = NFSMTOD(mb2, char *);
736ca35587Sdholland 		nd->nd_mb->m_next = mb2;
746ca35587Sdholland 		nd->nd_mb = mb2;
756ca35587Sdholland 	}
766ca35587Sdholland 	retp = (void *)(nd->nd_bpos);
776ca35587Sdholland 	nd->nd_mb->m_len += siz;
786ca35587Sdholland 	nd->nd_bpos += siz;
796ca35587Sdholland 	return (retp);
806ca35587Sdholland }
816ca35587Sdholland 
826ca35587Sdholland #define	NFSM_BUILD(a, c, s)	((a) = (c)nfsm_build(nd, (s)))
836ca35587Sdholland 
846ca35587Sdholland static __inline void *
nfsm_dissect(struct nfsrv_descript * nd,int siz)856ca35587Sdholland nfsm_dissect(struct nfsrv_descript *nd, int siz)
866ca35587Sdholland {
876ca35587Sdholland 	int tt1;
886ca35587Sdholland 	void *retp;
896ca35587Sdholland 
90*0342d508Spgoyette 	tt1 = NFSMTOD(nd->nd_md, char *) + nd->nd_md->m_len - nd->nd_dpos;
916ca35587Sdholland 	if (tt1 >= siz) {
926ca35587Sdholland 		retp = (void *)nd->nd_dpos;
936ca35587Sdholland 		nd->nd_dpos += siz;
946ca35587Sdholland 	} else {
956ca35587Sdholland 		retp = nfsm_dissct(nd, siz, M_WAITOK);
966ca35587Sdholland 	}
976ca35587Sdholland 	return (retp);
986ca35587Sdholland }
996ca35587Sdholland 
1006ca35587Sdholland static __inline void *
nfsm_dissect_nonblock(struct nfsrv_descript * nd,int siz)1016ca35587Sdholland nfsm_dissect_nonblock(struct nfsrv_descript *nd, int siz)
1026ca35587Sdholland {
1036ca35587Sdholland 	int tt1;
1046ca35587Sdholland 	void *retp;
1056ca35587Sdholland 
106*0342d508Spgoyette 	tt1 = NFSMTOD(nd->nd_md, char *) + nd->nd_md->m_len - nd->nd_dpos;
1076ca35587Sdholland 	if (tt1 >= siz) {
1086ca35587Sdholland 		retp = (void *)nd->nd_dpos;
1096ca35587Sdholland 		nd->nd_dpos += siz;
1106ca35587Sdholland 	} else {
1116ca35587Sdholland 		retp = nfsm_dissct(nd, siz, M_NOWAIT);
1126ca35587Sdholland 	}
1136ca35587Sdholland 	return (retp);
1146ca35587Sdholland }
1156ca35587Sdholland 
1166ca35587Sdholland #define	NFSM_DISSECT(a, c, s) 						\
1176ca35587Sdholland 	do {								\
1186ca35587Sdholland 		(a) = (c)nfsm_dissect(nd, (s));	 			\
1196ca35587Sdholland 		if ((a) == NULL) { 					\
1206ca35587Sdholland 			error = EBADRPC; 				\
1216ca35587Sdholland 			goto nfsmout; 					\
1226ca35587Sdholland 		}							\
1236ca35587Sdholland 	} while (0)
1246ca35587Sdholland 
1256ca35587Sdholland #define	NFSM_DISSECT_NONBLOCK(a, c, s) 					\
1266ca35587Sdholland 	do {								\
1276ca35587Sdholland 		(a) = (c)nfsm_dissect_nonblock(nd, (s));		\
1286ca35587Sdholland 		if ((a) == NULL) { 					\
1296ca35587Sdholland 			error = EBADRPC; 				\
1306ca35587Sdholland 			goto nfsmout; 					\
1316ca35587Sdholland 		}							\
1326ca35587Sdholland 	} while (0)
1336ca35587Sdholland #endif	/* !APPLE */
1346ca35587Sdholland 
1356ca35587Sdholland #define	NFSM_STRSIZ(s, m)  						\
1366ca35587Sdholland 	do {								\
1376ca35587Sdholland 		tl = (u_int32_t *)nfsm_dissect(nd, NFSX_UNSIGNED);	\
1386ca35587Sdholland 		if (!tl || ((s) = fxdr_unsigned(int32_t, *tl)) > (m)) { \
1396ca35587Sdholland 			error = EBADRPC; 				\
1406ca35587Sdholland 			goto nfsmout; 					\
1416ca35587Sdholland 		}							\
1426ca35587Sdholland 	} while (0)
1436ca35587Sdholland 
1446ca35587Sdholland #define	NFSM_RNDUP(a)	(((a)+3)&(~0x3))
1456ca35587Sdholland 
1466ca35587Sdholland #endif	/* _NFS_NFSM_SUBS_H_ */
147