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