xref: /titanic_41/usr/src/uts/common/rpc/xdr_mblk.c (revision c242f9a02a2ef021449275ae0a1d2581ee77231d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5742e7b7fSmaheshvs  * Common Development and Distribution License (the "License").
6742e7b7fSmaheshvs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * xdr_mblk.c, XDR implementation on kernel streams mblks.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/systm.h>
417c478bd9Sstevel@tonic-gate #include <sys/stream.h>
427c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
437c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
447c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <rpc/types.h>
497c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_getint32(XDR *, int32_t *);
527c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_putint32(XDR *, int32_t *);
537c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_getbytes(XDR *, caddr_t, int);
547c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_putbytes(XDR *, caddr_t, int);
557c478bd9Sstevel@tonic-gate static uint_t	xdrmblk_getpos(XDR *);
567c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_setpos(XDR *, uint_t);
577c478bd9Sstevel@tonic-gate static rpc_inline_t *xdrmblk_inline(XDR *, int);
587c478bd9Sstevel@tonic-gate static void	xdrmblk_destroy(XDR *);
597c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_control(XDR *, int, void *);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static mblk_t *xdrmblk_alloc(int);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * Xdr on mblks operations vector.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate struct	xdr_ops xdrmblk_ops = {
677c478bd9Sstevel@tonic-gate 	xdrmblk_getbytes,
687c478bd9Sstevel@tonic-gate 	xdrmblk_putbytes,
697c478bd9Sstevel@tonic-gate 	xdrmblk_getpos,
707c478bd9Sstevel@tonic-gate 	xdrmblk_setpos,
717c478bd9Sstevel@tonic-gate 	xdrmblk_inline,
727c478bd9Sstevel@tonic-gate 	xdrmblk_destroy,
737c478bd9Sstevel@tonic-gate 	xdrmblk_control,
747c478bd9Sstevel@tonic-gate 	xdrmblk_getint32,
757c478bd9Sstevel@tonic-gate 	xdrmblk_putint32
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Initialize xdr stream.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate void
xdrmblk_init(XDR * xdrs,mblk_t * m,enum xdr_op op,int sz)827c478bd9Sstevel@tonic-gate xdrmblk_init(XDR *xdrs, mblk_t *m, enum xdr_op op, int sz)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	xdrs->x_op = op;
857c478bd9Sstevel@tonic-gate 	xdrs->x_ops = &xdrmblk_ops;
867c478bd9Sstevel@tonic-gate 	xdrs->x_base = (caddr_t)m;
877c478bd9Sstevel@tonic-gate 	xdrs->x_public = NULL;
887c478bd9Sstevel@tonic-gate 	xdrs->x_private = (caddr_t)(uintptr_t)sz;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (op == XDR_DECODE)
917c478bd9Sstevel@tonic-gate 		xdrs->x_handy = (int)(m->b_wptr - m->b_rptr);
927c478bd9Sstevel@tonic-gate 	else
937c478bd9Sstevel@tonic-gate 		xdrs->x_handy = (int)(m->b_datap->db_lim - m->b_datap->db_base);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* ARGSUSED */
977c478bd9Sstevel@tonic-gate static void
xdrmblk_destroy(XDR * xdrs)987c478bd9Sstevel@tonic-gate xdrmblk_destroy(XDR *xdrs)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_getint32(XDR * xdrs,int32_t * int32p)1037c478bd9Sstevel@tonic-gate xdrmblk_getint32(XDR *xdrs, int32_t *int32p)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	mblk_t *m;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
1087c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
1097c478bd9Sstevel@tonic-gate 	if (m == NULL)
1107c478bd9Sstevel@tonic-gate 		return (FALSE);
1117c478bd9Sstevel@tonic-gate 	/*
1127c478bd9Sstevel@tonic-gate 	 * If the pointer is not aligned or there is not
1137c478bd9Sstevel@tonic-gate 	 * enough bytes, pullupmsg to get enough bytes and
1147c478bd9Sstevel@tonic-gate 	 * align the mblk.
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	if (!IS_P2ALIGNED(m->b_rptr, sizeof (int32_t)) ||
1177c478bd9Sstevel@tonic-gate 	    xdrs->x_handy < sizeof (int32_t)) {
1187c478bd9Sstevel@tonic-gate 		while (!pullupmsg(m, sizeof (int32_t))) {
1197c478bd9Sstevel@tonic-gate 			/*
1207c478bd9Sstevel@tonic-gate 			 * Could have failed due to not
1217c478bd9Sstevel@tonic-gate 			 * enough data or an allocb failure.
1227c478bd9Sstevel@tonic-gate 			 */
1237c478bd9Sstevel@tonic-gate 			if (xmsgsize(m) < sizeof (int32_t))
1247c478bd9Sstevel@tonic-gate 				return (FALSE);
1257c478bd9Sstevel@tonic-gate 			delay(hz);
1267c478bd9Sstevel@tonic-gate 		}
1277c478bd9Sstevel@tonic-gate 		xdrs->x_handy = (int)(m->b_wptr - m->b_rptr);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
1317c478bd9Sstevel@tonic-gate 	*int32p = ntohl(*((int32_t *)(m->b_rptr)));
1327c478bd9Sstevel@tonic-gate 	m->b_rptr += sizeof (int32_t);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/*
1357c478bd9Sstevel@tonic-gate 	 * Instead of leaving handy as 0 causing more pullupmsg's
1367c478bd9Sstevel@tonic-gate 	 * simply move to the next mblk.
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 	if ((xdrs->x_handy -= sizeof (int32_t)) == 0) {
1397c478bd9Sstevel@tonic-gate 		m = m->b_cont;
1407c478bd9Sstevel@tonic-gate 		xdrs->x_base = (caddr_t)m;
1417c478bd9Sstevel@tonic-gate 		if (m != NULL)
1427c478bd9Sstevel@tonic-gate 			xdrs->x_handy = (int)(m->b_wptr - m->b_rptr);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 	return (TRUE);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_putint32(XDR * xdrs,int32_t * int32p)1487c478bd9Sstevel@tonic-gate xdrmblk_putint32(XDR *xdrs, int32_t *int32p)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	mblk_t *m;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
1537c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
1547c478bd9Sstevel@tonic-gate 	if (m == NULL)
1557c478bd9Sstevel@tonic-gate 		return (FALSE);
1567c478bd9Sstevel@tonic-gate 	if ((xdrs->x_handy -= (int)sizeof (int32_t)) < 0) {
1577c478bd9Sstevel@tonic-gate 		if (m->b_cont == NULL) {
1587c478bd9Sstevel@tonic-gate 			m->b_cont = xdrmblk_alloc((int)(uintptr_t)
1597c478bd9Sstevel@tonic-gate 			    xdrs->x_private);
1607c478bd9Sstevel@tonic-gate 		}
1617c478bd9Sstevel@tonic-gate 		m = m->b_cont;
1627c478bd9Sstevel@tonic-gate 		xdrs->x_base = (caddr_t)m;
1637c478bd9Sstevel@tonic-gate 		if (m == NULL) {
1647c478bd9Sstevel@tonic-gate 			xdrs->x_handy = 0;
1657c478bd9Sstevel@tonic-gate 			return (FALSE);
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 		xdrs->x_handy = (int)(m->b_datap->db_lim - m->b_rptr -
1687c478bd9Sstevel@tonic-gate 		    sizeof (int32_t));
1697c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr == m->b_wptr);
1707c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr >= m->b_datap->db_base);
1717c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr < m->b_datap->db_lim);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
1747c478bd9Sstevel@tonic-gate 	*(int32_t *)m->b_wptr = htonl(*int32p);
1757c478bd9Sstevel@tonic-gate 	m->b_wptr += sizeof (int32_t);
1767c478bd9Sstevel@tonic-gate 	ASSERT(m->b_wptr <= m->b_datap->db_lim);
1777c478bd9Sstevel@tonic-gate 	return (TRUE);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * We pick 16 as a compromise threshold for most architectures.
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate #define	XDRMBLK_BCOPY_LIMIT	16
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_getbytes(XDR * xdrs,caddr_t addr,int len)1867c478bd9Sstevel@tonic-gate xdrmblk_getbytes(XDR *xdrs, caddr_t addr, int len)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	mblk_t *m;
1897c478bd9Sstevel@tonic-gate 	int i;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
1927c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
1937c478bd9Sstevel@tonic-gate 	if (m == NULL)
1947c478bd9Sstevel@tonic-gate 		return (FALSE);
1957c478bd9Sstevel@tonic-gate 	/*
1967c478bd9Sstevel@tonic-gate 	 * Performance tweak: converted explicit bcopy()
1977c478bd9Sstevel@tonic-gate 	 * call to simple in-line. This function is called
1987c478bd9Sstevel@tonic-gate 	 * to process things like readdir reply filenames
1997c478bd9Sstevel@tonic-gate 	 * which are small strings--typically 12 bytes or less.
2007c478bd9Sstevel@tonic-gate 	 * Overhead of calling bcopy() is obnoxious for such
2017c478bd9Sstevel@tonic-gate 	 * small copies.
2027c478bd9Sstevel@tonic-gate 	 */
2037c478bd9Sstevel@tonic-gate 	while ((xdrs->x_handy -= len) < 0) {
2047c478bd9Sstevel@tonic-gate 		if ((xdrs->x_handy += len) > 0) {
2057c478bd9Sstevel@tonic-gate 			if (len < XDRMBLK_BCOPY_LIMIT) {
2067c478bd9Sstevel@tonic-gate 				for (i = 0; i < xdrs->x_handy; i++)
2077c478bd9Sstevel@tonic-gate 					*addr++ = *m->b_rptr++;
2087c478bd9Sstevel@tonic-gate 			} else {
2097c478bd9Sstevel@tonic-gate 				bcopy(m->b_rptr, addr, xdrs->x_handy);
2107c478bd9Sstevel@tonic-gate 				m->b_rptr += xdrs->x_handy;
2117c478bd9Sstevel@tonic-gate 				addr += xdrs->x_handy;
2127c478bd9Sstevel@tonic-gate 			}
2137c478bd9Sstevel@tonic-gate 			len -= xdrs->x_handy;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		m = m->b_cont;
2167c478bd9Sstevel@tonic-gate 		xdrs->x_base = (caddr_t)m;
2177c478bd9Sstevel@tonic-gate 		if (m == NULL) {
2187c478bd9Sstevel@tonic-gate 			xdrs->x_handy = 0;
2197c478bd9Sstevel@tonic-gate 			return (FALSE);
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 		xdrs->x_handy = (int)(m->b_wptr - m->b_rptr);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	if (len < XDRMBLK_BCOPY_LIMIT) {
2247c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++)
2257c478bd9Sstevel@tonic-gate 			*addr++ = *m->b_rptr++;
2267c478bd9Sstevel@tonic-gate 	} else {
2277c478bd9Sstevel@tonic-gate 		bcopy(m->b_rptr, addr, len);
2287c478bd9Sstevel@tonic-gate 		m->b_rptr += len;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	return (TRUE);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * Sort of like getbytes except that instead of getting bytes we return the
2357c478bd9Sstevel@tonic-gate  * mblk chain which contains the data.  If the data ends in the middle of
2367c478bd9Sstevel@tonic-gate  * an mblk, the mblk is dup'd and split, so that the data will end on an
2377c478bd9Sstevel@tonic-gate  * mblk.  Note that it is up to the caller to keep track of the data length
2387c478bd9Sstevel@tonic-gate  * and not walk too far down the mblk chain.
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate bool_t
xdrmblk_getmblk(XDR * xdrs,mblk_t ** mm,uint_t * lenp)2427c478bd9Sstevel@tonic-gate xdrmblk_getmblk(XDR *xdrs, mblk_t **mm, uint_t *lenp)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	mblk_t *m, *nextm;
2457c478bd9Sstevel@tonic-gate 	int len;
2467c478bd9Sstevel@tonic-gate 	int32_t llen;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (!xdrmblk_getint32(xdrs, &llen))
2497c478bd9Sstevel@tonic-gate 		return (FALSE);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	*lenp = llen;
2527c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
2537c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
2547c478bd9Sstevel@tonic-gate 	*mm = m;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * Walk the mblk chain until we get to the end or we've gathered
2587c478bd9Sstevel@tonic-gate 	 * enough data.
2597c478bd9Sstevel@tonic-gate 	 */
2607c478bd9Sstevel@tonic-gate 	len = 0;
2617c478bd9Sstevel@tonic-gate 	llen = roundup(llen, BYTES_PER_XDR_UNIT);
2627c478bd9Sstevel@tonic-gate 	while (m != NULL && len + (int)MBLKL(m) <= llen) {
2637c478bd9Sstevel@tonic-gate 		len += (int)MBLKL(m);
2647c478bd9Sstevel@tonic-gate 		m = m->b_cont;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	if (len < llen) {
2677c478bd9Sstevel@tonic-gate 		if (m == NULL) {
2687c478bd9Sstevel@tonic-gate 			return (FALSE);
2697c478bd9Sstevel@tonic-gate 		} else {
2707c478bd9Sstevel@tonic-gate 			int tail_bytes = llen - len;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 			/*
2737c478bd9Sstevel@tonic-gate 			 * Split the mblk with the last chunk of data and
2747c478bd9Sstevel@tonic-gate 			 * insert it into the chain.  The new mblk goes
2757c478bd9Sstevel@tonic-gate 			 * after the existing one so that it will get freed
2767c478bd9Sstevel@tonic-gate 			 * properly.
2777c478bd9Sstevel@tonic-gate 			 */
2787c478bd9Sstevel@tonic-gate 			nextm = dupb(m);
2797c478bd9Sstevel@tonic-gate 			if (nextm == NULL)
2807c478bd9Sstevel@tonic-gate 				return (FALSE);
2817c478bd9Sstevel@tonic-gate 			nextm->b_cont = m->b_cont;
2827c478bd9Sstevel@tonic-gate 			m->b_cont = nextm;
2837c478bd9Sstevel@tonic-gate 			m->b_wptr = m->b_rptr + tail_bytes;
2847c478bd9Sstevel@tonic-gate 			nextm->b_rptr += tail_bytes;
2857c478bd9Sstevel@tonic-gate 			ASSERT(nextm->b_rptr != nextm->b_wptr);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			m = nextm;	/* for x_base */
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 	xdrs->x_base = (caddr_t)m;
2917c478bd9Sstevel@tonic-gate 	xdrs->x_handy = m != NULL ? MBLKL(m) : 0;
2927c478bd9Sstevel@tonic-gate 	return (TRUE);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_putbytes(XDR * xdrs,caddr_t addr,int len)2967c478bd9Sstevel@tonic-gate xdrmblk_putbytes(XDR *xdrs, caddr_t addr, int len)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	mblk_t *m;
2997c478bd9Sstevel@tonic-gate 	uint_t i;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
3027c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
3037c478bd9Sstevel@tonic-gate 	if (m == NULL)
3047c478bd9Sstevel@tonic-gate 		return (FALSE);
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * Performance tweak: converted explicit bcopy()
3077c478bd9Sstevel@tonic-gate 	 * call to simple in-line. This function is called
3087c478bd9Sstevel@tonic-gate 	 * to process things like readdir reply filenames
3097c478bd9Sstevel@tonic-gate 	 * which are small strings--typically 12 bytes or less.
3107c478bd9Sstevel@tonic-gate 	 * Overhead of calling bcopy() is obnoxious for such
3117c478bd9Sstevel@tonic-gate 	 * small copies.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	while ((xdrs->x_handy -= len) < 0) {
3147c478bd9Sstevel@tonic-gate 		if ((xdrs->x_handy += len) > 0) {
3157c478bd9Sstevel@tonic-gate 			if (xdrs->x_handy < XDRMBLK_BCOPY_LIMIT) {
3167c478bd9Sstevel@tonic-gate 				for (i = 0; i < (uint_t)xdrs->x_handy; i++)
3177c478bd9Sstevel@tonic-gate 					*m->b_wptr++ = *addr++;
3187c478bd9Sstevel@tonic-gate 			} else {
3197c478bd9Sstevel@tonic-gate 				bcopy(addr, m->b_wptr, xdrs->x_handy);
3207c478bd9Sstevel@tonic-gate 				m->b_wptr += xdrs->x_handy;
3217c478bd9Sstevel@tonic-gate 				addr += xdrs->x_handy;
3227c478bd9Sstevel@tonic-gate 			}
3237c478bd9Sstevel@tonic-gate 			len -= xdrs->x_handy;
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		/*
3277c478bd9Sstevel@tonic-gate 		 * We don't have enough space, so allocate the
3287c478bd9Sstevel@tonic-gate 		 * amount we need, or x_private, whichever is larger.
3297c478bd9Sstevel@tonic-gate 		 * It is better to let the underlying transport divide
3307c478bd9Sstevel@tonic-gate 		 * large chunks than to try and guess what is best.
3317c478bd9Sstevel@tonic-gate 		 */
3327c478bd9Sstevel@tonic-gate 		if (m->b_cont == NULL)
3337c478bd9Sstevel@tonic-gate 			m->b_cont = xdrmblk_alloc(MAX(len,
3347c478bd9Sstevel@tonic-gate 			    (int)(uintptr_t)xdrs->x_private));
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		m = m->b_cont;
3377c478bd9Sstevel@tonic-gate 		xdrs->x_base = (caddr_t)m;
3387c478bd9Sstevel@tonic-gate 		if (m == NULL) {
3397c478bd9Sstevel@tonic-gate 			xdrs->x_handy = 0;
3407c478bd9Sstevel@tonic-gate 			return (FALSE);
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 		xdrs->x_handy = (int)(m->b_datap->db_lim - m->b_rptr);
3437c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr == m->b_wptr);
3447c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr >= m->b_datap->db_base);
3457c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr < m->b_datap->db_lim);
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 	if (len < XDRMBLK_BCOPY_LIMIT) {
3487c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++)
3497c478bd9Sstevel@tonic-gate 			*m->b_wptr++ = *addr++;
3507c478bd9Sstevel@tonic-gate 	} else {
3517c478bd9Sstevel@tonic-gate 		bcopy(addr, m->b_wptr, len);
3527c478bd9Sstevel@tonic-gate 		m->b_wptr += len;
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 	ASSERT(m->b_wptr <= m->b_datap->db_lim);
3557c478bd9Sstevel@tonic-gate 	return (TRUE);
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * We avoid a copy by merely adding this mblk to the list.  The caller is
3607c478bd9Sstevel@tonic-gate  * responsible for allocating and filling in the mblk. If len is
3617c478bd9Sstevel@tonic-gate  * not a multiple of BYTES_PER_XDR_UNIT, the caller has the option
3627c478bd9Sstevel@tonic-gate  * of making the data a BYTES_PER_XDR_UNIT multiple (b_wptr - b_rptr is
3637c478bd9Sstevel@tonic-gate  * a BYTES_PER_XDR_UNIT multiple), but in this case the caller has to ensure
364*c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * that the filler bytes are initialized to zero.
3657c478bd9Sstevel@tonic-gate  */
3667c478bd9Sstevel@tonic-gate bool_t
xdrmblk_putmblk(XDR * xdrs,mblk_t * m,uint_t len)3677c478bd9Sstevel@tonic-gate xdrmblk_putmblk(XDR *xdrs, mblk_t *m, uint_t len)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	int32_t llen = (int32_t)len;
3707c478bd9Sstevel@tonic-gate 
371*c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	if ((DLEN(m) % BYTES_PER_XDR_UNIT) != 0)
3727c478bd9Sstevel@tonic-gate 		return (FALSE);
3737c478bd9Sstevel@tonic-gate 	if (!xdrmblk_putint32(xdrs, &llen))
3747c478bd9Sstevel@tonic-gate 		return (FALSE);
375*c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
3767c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
3777c478bd9Sstevel@tonic-gate 	((mblk_t *)xdrs->x_base)->b_cont = m;
378*c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
379*c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	/* base points to the last mblk */
380*c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	while (m->b_cont)
381*c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		m = m->b_cont;
3827c478bd9Sstevel@tonic-gate 	xdrs->x_base = (caddr_t)m;
3837c478bd9Sstevel@tonic-gate 	xdrs->x_handy = 0;
3847c478bd9Sstevel@tonic-gate 	return (TRUE);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate static uint_t
xdrmblk_getpos(XDR * xdrs)3887c478bd9Sstevel@tonic-gate xdrmblk_getpos(XDR *xdrs)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	uint_t tmp;
3917c478bd9Sstevel@tonic-gate 	mblk_t *m;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
3947c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_DECODE)
3977c478bd9Sstevel@tonic-gate 		tmp = (uint_t)(m->b_rptr - m->b_datap->db_base);
3987c478bd9Sstevel@tonic-gate 	else
3997c478bd9Sstevel@tonic-gate 		tmp = (uint_t)(m->b_wptr - m->b_datap->db_base);
4007c478bd9Sstevel@tonic-gate 	return (tmp);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_setpos(XDR * xdrs,uint_t pos)4057c478bd9Sstevel@tonic-gate xdrmblk_setpos(XDR *xdrs, uint_t pos)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate 	mblk_t *m;
4087c478bd9Sstevel@tonic-gate 	unsigned char *newaddr;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
4117c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
4127c478bd9Sstevel@tonic-gate 	if (m == NULL)
4137c478bd9Sstevel@tonic-gate 		return (FALSE);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/* calculate the new address from the base */
4167c478bd9Sstevel@tonic-gate 	newaddr = m->b_datap->db_base + pos;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_DECODE) {
4197c478bd9Sstevel@tonic-gate 		if (newaddr > m->b_wptr)
4207c478bd9Sstevel@tonic-gate 			return (FALSE);
4217c478bd9Sstevel@tonic-gate 		m->b_rptr = newaddr;
4227c478bd9Sstevel@tonic-gate 		xdrs->x_handy = (int)(m->b_wptr - newaddr);
4237c478bd9Sstevel@tonic-gate 	} else {
4247c478bd9Sstevel@tonic-gate 		if (newaddr > m->b_datap->db_lim)
4257c478bd9Sstevel@tonic-gate 			return (FALSE);
4267c478bd9Sstevel@tonic-gate 		m->b_wptr = newaddr;
4277c478bd9Sstevel@tonic-gate 		xdrs->x_handy = (int)(m->b_datap->db_lim - newaddr);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	return (TRUE);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate #ifdef DEBUG
4347c478bd9Sstevel@tonic-gate static int xdrmblk_inline_hits = 0;
4357c478bd9Sstevel@tonic-gate static int xdrmblk_inline_misses = 0;
4367c478bd9Sstevel@tonic-gate static int do_xdrmblk_inline = 1;
4377c478bd9Sstevel@tonic-gate #endif
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate static rpc_inline_t *
xdrmblk_inline(XDR * xdrs,int len)4407c478bd9Sstevel@tonic-gate xdrmblk_inline(XDR *xdrs, int len)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	rpc_inline_t *buf;
4437c478bd9Sstevel@tonic-gate 	mblk_t *m;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	/*
4467c478bd9Sstevel@tonic-gate 	 * Can't inline XDR_FREE calls, doesn't make sense.
4477c478bd9Sstevel@tonic-gate 	 */
4487c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_FREE)
4497c478bd9Sstevel@tonic-gate 		return (NULL);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * Can't inline if there isn't enough room, don't have an
4537c478bd9Sstevel@tonic-gate 	 * mblk pointer, its not 4 byte aligned, or if there is more than
4547c478bd9Sstevel@tonic-gate 	 * one reference to the data block associated with this mblk.  This last
4557c478bd9Sstevel@tonic-gate 	 * check is used because the caller may want to modified
4567c478bd9Sstevel@tonic-gate 	 * the data in the inlined portion and someone else is
4577c478bd9Sstevel@tonic-gate 	 * holding a reference to the data who may not want it
4587c478bd9Sstevel@tonic-gate 	 * to be modified.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	if (xdrs->x_handy < len ||
4617c478bd9Sstevel@tonic-gate 	    /* LINTED pointer alignment */
4627c478bd9Sstevel@tonic-gate 	    (m = (mblk_t *)xdrs->x_base) == NULL ||
4637c478bd9Sstevel@tonic-gate 	    !IS_P2ALIGNED(m->b_rptr, sizeof (int32_t)) ||
4647c478bd9Sstevel@tonic-gate 	    m->b_datap->db_ref != 1) {
4657c478bd9Sstevel@tonic-gate #ifdef DEBUG
4667c478bd9Sstevel@tonic-gate 		xdrmblk_inline_misses++;
4677c478bd9Sstevel@tonic-gate #endif
4687c478bd9Sstevel@tonic-gate 		return (NULL);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate #ifdef DEBUG
4727c478bd9Sstevel@tonic-gate 	if (!do_xdrmblk_inline) {
4737c478bd9Sstevel@tonic-gate 		xdrmblk_inline_misses++;
4747c478bd9Sstevel@tonic-gate 		return (NULL);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate #endif
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	xdrs->x_handy -= len;
4797c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_DECODE) {
4807c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
4817c478bd9Sstevel@tonic-gate 		buf = (rpc_inline_t *)m->b_rptr;
4827c478bd9Sstevel@tonic-gate 		m->b_rptr += len;
4837c478bd9Sstevel@tonic-gate 	} else {
4847c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
4857c478bd9Sstevel@tonic-gate 		buf = (rpc_inline_t *)m->b_wptr;
4867c478bd9Sstevel@tonic-gate 		m->b_wptr += len;
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate #ifdef DEBUG
4897c478bd9Sstevel@tonic-gate 	xdrmblk_inline_hits++;
4907c478bd9Sstevel@tonic-gate #endif
4917c478bd9Sstevel@tonic-gate 	return (buf);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_control(XDR * xdrs,int request,void * info)4957c478bd9Sstevel@tonic-gate xdrmblk_control(XDR *xdrs, int request, void *info)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	mblk_t *m;
4987c478bd9Sstevel@tonic-gate 	int32_t *int32p;
4997c478bd9Sstevel@tonic-gate 	int len;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	switch (request) {
5027c478bd9Sstevel@tonic-gate 	case XDR_PEEK:
5037c478bd9Sstevel@tonic-gate 		/*
5047c478bd9Sstevel@tonic-gate 		 * Return the next 4 byte unit in the XDR stream.
5057c478bd9Sstevel@tonic-gate 		 */
5067c478bd9Sstevel@tonic-gate 		if (xdrs->x_handy < sizeof (int32_t))
5077c478bd9Sstevel@tonic-gate 			return (FALSE);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
5107c478bd9Sstevel@tonic-gate 		m = (mblk_t *)xdrs->x_base;
5117c478bd9Sstevel@tonic-gate 		if (m == NULL)
5127c478bd9Sstevel@tonic-gate 			return (FALSE);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		/*
5157c478bd9Sstevel@tonic-gate 		 * If the pointer is not aligned, fail the peek
5167c478bd9Sstevel@tonic-gate 		 */
5177c478bd9Sstevel@tonic-gate 		if (!IS_P2ALIGNED(m->b_rptr, sizeof (int32_t)))
5187c478bd9Sstevel@tonic-gate 			return (FALSE);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		int32p = (int32_t *)info;
5217c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
5227c478bd9Sstevel@tonic-gate 		*int32p = ntohl(*((int32_t *)(m->b_rptr)));
5237c478bd9Sstevel@tonic-gate 		return (TRUE);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	case XDR_SKIPBYTES:
5267c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
5277c478bd9Sstevel@tonic-gate 		m = (mblk_t *)xdrs->x_base;
5287c478bd9Sstevel@tonic-gate 		if (m == NULL)
5297c478bd9Sstevel@tonic-gate 			return (FALSE);
5307c478bd9Sstevel@tonic-gate 		int32p = (int32_t *)info;
5317c478bd9Sstevel@tonic-gate 		len = RNDUP((int)(*int32p));
532742e7b7fSmaheshvs 		if (len < 0)
533742e7b7fSmaheshvs 			return (FALSE);
5347c478bd9Sstevel@tonic-gate 		while ((xdrs->x_handy -= len) < 0) {
5357c478bd9Sstevel@tonic-gate 			if ((xdrs->x_handy += len) > 0) {
5367c478bd9Sstevel@tonic-gate 				m->b_rptr += xdrs->x_handy;
5377c478bd9Sstevel@tonic-gate 				len -= xdrs->x_handy;
5387c478bd9Sstevel@tonic-gate 			}
5397c478bd9Sstevel@tonic-gate 			m = m->b_cont;
5407c478bd9Sstevel@tonic-gate 			xdrs->x_base = (caddr_t)m;
5417c478bd9Sstevel@tonic-gate 			if (m == NULL) {
5427c478bd9Sstevel@tonic-gate 				xdrs->x_handy = 0;
5437c478bd9Sstevel@tonic-gate 				return (FALSE);
5447c478bd9Sstevel@tonic-gate 			}
5457c478bd9Sstevel@tonic-gate 			xdrs->x_handy = (int)(m->b_wptr - m->b_rptr);
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 		m->b_rptr += len;
5487c478bd9Sstevel@tonic-gate 		return (TRUE);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	default:
5517c478bd9Sstevel@tonic-gate 		return (FALSE);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate #define	HDR_SPACE	128
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate static mblk_t *
xdrmblk_alloc(int sz)5587c478bd9Sstevel@tonic-gate xdrmblk_alloc(int sz)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	mblk_t *mp;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (sz == 0)
5637c478bd9Sstevel@tonic-gate 		return (NULL);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/*
5667c478bd9Sstevel@tonic-gate 	 * Pad the front of the message to allow the lower networking
5677c478bd9Sstevel@tonic-gate 	 * layers space to add headers as needed.
5687c478bd9Sstevel@tonic-gate 	 */
5697c478bd9Sstevel@tonic-gate 	sz += HDR_SPACE;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	while ((mp = allocb(sz, BPRI_LO)) == NULL) {
5727c478bd9Sstevel@tonic-gate 		if (strwaitbuf(sz, BPRI_LO))
5737c478bd9Sstevel@tonic-gate 			return (NULL);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	mp->b_wptr += HDR_SPACE;
5777c478bd9Sstevel@tonic-gate 	mp->b_rptr = mp->b_wptr;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	return (mp);
5807c478bd9Sstevel@tonic-gate }
581