xref: /illumos-gate/usr/src/uts/common/rpc/xdr_mblk.c (revision 5dde82e7)
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  */
21cf98b944SMarcel Telka 
22cf98b944SMarcel Telka /*
23*5dde82e7SMarcel Telka  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
24cf98b944SMarcel Telka  */
25cf98b944SMarcel Telka 
267c478bd9Sstevel@tonic-gate /*
27c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
327c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
367c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * xdr_mblk.c, XDR implementation on kernel streams mblks.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
467c478bd9Sstevel@tonic-gate #include <sys/stream.h>
477c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
487c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
497c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
507c478bd9Sstevel@tonic-gate #include <sys/debug.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <rpc/types.h>
547c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_getint32(XDR *, int32_t *);
577c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_putint32(XDR *, int32_t *);
587c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_getbytes(XDR *, caddr_t, int);
597c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_putbytes(XDR *, caddr_t, int);
607c478bd9Sstevel@tonic-gate static uint_t	xdrmblk_getpos(XDR *);
617c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_setpos(XDR *, uint_t);
627c478bd9Sstevel@tonic-gate static rpc_inline_t *xdrmblk_inline(XDR *, int);
637c478bd9Sstevel@tonic-gate static void	xdrmblk_destroy(XDR *);
647c478bd9Sstevel@tonic-gate static bool_t	xdrmblk_control(XDR *, int, void *);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static mblk_t *xdrmblk_alloc(int);
67cf98b944SMarcel Telka static void xdrmblk_skip_fully_read_mblks(XDR *);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * Xdr on mblks operations vector.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate struct	xdr_ops xdrmblk_ops = {
737c478bd9Sstevel@tonic-gate 	xdrmblk_getbytes,
747c478bd9Sstevel@tonic-gate 	xdrmblk_putbytes,
757c478bd9Sstevel@tonic-gate 	xdrmblk_getpos,
767c478bd9Sstevel@tonic-gate 	xdrmblk_setpos,
777c478bd9Sstevel@tonic-gate 	xdrmblk_inline,
787c478bd9Sstevel@tonic-gate 	xdrmblk_destroy,
797c478bd9Sstevel@tonic-gate 	xdrmblk_control,
807c478bd9Sstevel@tonic-gate 	xdrmblk_getint32,
817c478bd9Sstevel@tonic-gate 	xdrmblk_putint32
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
84cf98b944SMarcel Telka /*
85cf98b944SMarcel Telka  * The xdrmblk_params structure holds the internal data for the XDR stream.
86cf98b944SMarcel Telka  * The x_private member of the XDR points to this structure.  The
87cf98b944SMarcel Telka  * xdrmblk_params structure is dynamically allocated in xdrmblk_init() and
88cf98b944SMarcel Telka  * freed in xdrmblk_destroy().
89cf98b944SMarcel Telka  *
90cf98b944SMarcel Telka  * The apos and rpos members of the xdrmblk_params structure are used to
91cf98b944SMarcel Telka  * implement xdrmblk_getpos() and xdrmblk_setpos().
92cf98b944SMarcel Telka  *
93cf98b944SMarcel Telka  * In addition to the xdrmblk_params structure we store some additional
94cf98b944SMarcel Telka  * internal data directly in the XDR stream structure:
95cf98b944SMarcel Telka  *
96cf98b944SMarcel Telka  * x_base	A pointer to the current mblk (that one we are currently
97cf98b944SMarcel Telka  * 		working with).
98cf98b944SMarcel Telka  * x_handy	The number of available bytes (either for read or for write) in
99cf98b944SMarcel Telka  * 		the current mblk.
100cf98b944SMarcel Telka  */
101cf98b944SMarcel Telka struct xdrmblk_params {
102cf98b944SMarcel Telka 	int sz;
103cf98b944SMarcel Telka 	uint_t apos;	/* Absolute position of the current mblk */
104cf98b944SMarcel Telka 	uint_t rpos;	/* Relative position in the current mblk */
105cf98b944SMarcel Telka };
106cf98b944SMarcel Telka 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Initialize xdr stream.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate void
xdrmblk_init(XDR * xdrs,mblk_t * m,enum xdr_op op,int sz)1117c478bd9Sstevel@tonic-gate xdrmblk_init(XDR *xdrs, mblk_t *m, enum xdr_op op, int sz)
1127c478bd9Sstevel@tonic-gate {
113cf98b944SMarcel Telka 	struct xdrmblk_params *p;
114cf98b944SMarcel Telka 
1157c478bd9Sstevel@tonic-gate 	xdrs->x_op = op;
1167c478bd9Sstevel@tonic-gate 	xdrs->x_ops = &xdrmblk_ops;
1177c478bd9Sstevel@tonic-gate 	xdrs->x_base = (caddr_t)m;
1187c478bd9Sstevel@tonic-gate 	xdrs->x_public = NULL;
119cf98b944SMarcel Telka 	p = kmem_alloc(sizeof (struct xdrmblk_params), KM_SLEEP);
120cf98b944SMarcel Telka 	xdrs->x_private = (caddr_t)p;
1217c478bd9Sstevel@tonic-gate 
122cf98b944SMarcel Telka 	p->sz = sz;
123cf98b944SMarcel Telka 	p->apos = 0;
124cf98b944SMarcel Telka 	p->rpos = 0;
125cf98b944SMarcel Telka 
126cf98b944SMarcel Telka 	if (op == XDR_DECODE) {
127cf98b944SMarcel Telka 		xdrs->x_handy = (int)MBLKL(m);
128cf98b944SMarcel Telka 	} else {
129cf98b944SMarcel Telka 		xdrs->x_handy = (int)MBLKTAIL(m);
130cf98b944SMarcel Telka 		if (p->sz < sizeof (int32_t))
131cf98b944SMarcel Telka 			p->sz = sizeof (int32_t);
132cf98b944SMarcel Telka 	}
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static void
xdrmblk_destroy(XDR * xdrs)1367c478bd9Sstevel@tonic-gate xdrmblk_destroy(XDR *xdrs)
1377c478bd9Sstevel@tonic-gate {
138cf98b944SMarcel Telka 	kmem_free(xdrs->x_private, sizeof (struct xdrmblk_params));
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_getint32(XDR * xdrs,int32_t * int32p)1427c478bd9Sstevel@tonic-gate xdrmblk_getint32(XDR *xdrs, int32_t *int32p)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	mblk_t *m;
145cf98b944SMarcel Telka 	struct xdrmblk_params *p;
146cf98b944SMarcel Telka 
147cf98b944SMarcel Telka 	xdrmblk_skip_fully_read_mblks(xdrs);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
1507c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
1517c478bd9Sstevel@tonic-gate 	if (m == NULL)
1527c478bd9Sstevel@tonic-gate 		return (FALSE);
153cf98b944SMarcel Telka 
154cf98b944SMarcel Telka 	p = (struct xdrmblk_params *)xdrs->x_private;
155cf98b944SMarcel Telka 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * If the pointer is not aligned or there is not
1587c478bd9Sstevel@tonic-gate 	 * enough bytes, pullupmsg to get enough bytes and
1597c478bd9Sstevel@tonic-gate 	 * align the mblk.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	if (!IS_P2ALIGNED(m->b_rptr, sizeof (int32_t)) ||
1620a701b1eSRobert Gordon 	    xdrs->x_handy < sizeof (int32_t)) {
1637c478bd9Sstevel@tonic-gate 		while (!pullupmsg(m, sizeof (int32_t))) {
1647c478bd9Sstevel@tonic-gate 			/*
1657c478bd9Sstevel@tonic-gate 			 * Could have failed due to not
1667c478bd9Sstevel@tonic-gate 			 * enough data or an allocb failure.
1677c478bd9Sstevel@tonic-gate 			 */
1687c478bd9Sstevel@tonic-gate 			if (xmsgsize(m) < sizeof (int32_t))
1697c478bd9Sstevel@tonic-gate 				return (FALSE);
1707c478bd9Sstevel@tonic-gate 			delay(hz);
1717c478bd9Sstevel@tonic-gate 		}
172cf98b944SMarcel Telka 		p->apos += p->rpos;
173cf98b944SMarcel Telka 		p->rpos = 0;
174cf98b944SMarcel Telka 		xdrs->x_handy = (int)MBLKL(m);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
1787c478bd9Sstevel@tonic-gate 	*int32p = ntohl(*((int32_t *)(m->b_rptr)));
1797c478bd9Sstevel@tonic-gate 	m->b_rptr += sizeof (int32_t);
180cf98b944SMarcel Telka 	xdrs->x_handy -= sizeof (int32_t);
181cf98b944SMarcel Telka 	p->rpos += sizeof (int32_t);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return (TRUE);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_putint32(XDR * xdrs,int32_t * int32p)1877c478bd9Sstevel@tonic-gate xdrmblk_putint32(XDR *xdrs, int32_t *int32p)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	mblk_t *m;
190cf98b944SMarcel Telka 	struct xdrmblk_params *p;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
1937c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
1947c478bd9Sstevel@tonic-gate 	if (m == NULL)
1957c478bd9Sstevel@tonic-gate 		return (FALSE);
196cf98b944SMarcel Telka 
197cf98b944SMarcel Telka 	p = (struct xdrmblk_params *)xdrs->x_private;
198cf98b944SMarcel Telka 
199cf98b944SMarcel Telka 	while (!IS_P2ALIGNED(m->b_wptr, sizeof (int32_t)) ||
200cf98b944SMarcel Telka 	    xdrs->x_handy < sizeof (int32_t)) {
2017c478bd9Sstevel@tonic-gate 		if (m->b_cont == NULL) {
202cf98b944SMarcel Telka 			ASSERT(p->sz >= sizeof (int32_t));
203cf98b944SMarcel Telka 			m->b_cont = xdrmblk_alloc(p->sz);
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 		m = m->b_cont;
2067c478bd9Sstevel@tonic-gate 		xdrs->x_base = (caddr_t)m;
207cf98b944SMarcel Telka 		p->apos += p->rpos;
208cf98b944SMarcel Telka 		p->rpos = 0;
2097c478bd9Sstevel@tonic-gate 		if (m == NULL) {
2107c478bd9Sstevel@tonic-gate 			xdrs->x_handy = 0;
2117c478bd9Sstevel@tonic-gate 			return (FALSE);
2127c478bd9Sstevel@tonic-gate 		}
213cf98b944SMarcel Telka 		xdrs->x_handy = (int)MBLKTAIL(m);
2147c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr == m->b_wptr);
2157c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr >= m->b_datap->db_base);
2167c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr < m->b_datap->db_lim);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
2197c478bd9Sstevel@tonic-gate 	*(int32_t *)m->b_wptr = htonl(*int32p);
2207c478bd9Sstevel@tonic-gate 	m->b_wptr += sizeof (int32_t);
221cf98b944SMarcel Telka 	xdrs->x_handy -= sizeof (int32_t);
222cf98b944SMarcel Telka 	p->rpos += sizeof (int32_t);
2237c478bd9Sstevel@tonic-gate 	ASSERT(m->b_wptr <= m->b_datap->db_lim);
2247c478bd9Sstevel@tonic-gate 	return (TRUE);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * We pick 16 as a compromise threshold for most architectures.
2297c478bd9Sstevel@tonic-gate  */
2307c478bd9Sstevel@tonic-gate #define	XDRMBLK_BCOPY_LIMIT	16
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_getbytes(XDR * xdrs,caddr_t addr,int len)2337c478bd9Sstevel@tonic-gate xdrmblk_getbytes(XDR *xdrs, caddr_t addr, int len)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	mblk_t *m;
236cf98b944SMarcel Telka 	struct xdrmblk_params *p;
2377c478bd9Sstevel@tonic-gate 	int i;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
2407c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
2417c478bd9Sstevel@tonic-gate 	if (m == NULL)
2427c478bd9Sstevel@tonic-gate 		return (FALSE);
243cf98b944SMarcel Telka 
244cf98b944SMarcel Telka 	p = (struct xdrmblk_params *)xdrs->x_private;
245cf98b944SMarcel Telka 
2467c478bd9Sstevel@tonic-gate 	/*
2477c478bd9Sstevel@tonic-gate 	 * Performance tweak: converted explicit bcopy()
2487c478bd9Sstevel@tonic-gate 	 * call to simple in-line. This function is called
2497c478bd9Sstevel@tonic-gate 	 * to process things like readdir reply filenames
2507c478bd9Sstevel@tonic-gate 	 * which are small strings--typically 12 bytes or less.
2517c478bd9Sstevel@tonic-gate 	 * Overhead of calling bcopy() is obnoxious for such
2527c478bd9Sstevel@tonic-gate 	 * small copies.
2537c478bd9Sstevel@tonic-gate 	 */
254cf98b944SMarcel Telka 	while (xdrs->x_handy < len) {
255cf98b944SMarcel Telka 		if (xdrs->x_handy > 0) {
256cf98b944SMarcel Telka 			if (xdrs->x_handy < XDRMBLK_BCOPY_LIMIT) {
2577c478bd9Sstevel@tonic-gate 				for (i = 0; i < xdrs->x_handy; i++)
2587c478bd9Sstevel@tonic-gate 					*addr++ = *m->b_rptr++;
2597c478bd9Sstevel@tonic-gate 			} else {
2607c478bd9Sstevel@tonic-gate 				bcopy(m->b_rptr, addr, xdrs->x_handy);
2617c478bd9Sstevel@tonic-gate 				m->b_rptr += xdrs->x_handy;
2627c478bd9Sstevel@tonic-gate 				addr += xdrs->x_handy;
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 			len -= xdrs->x_handy;
265cf98b944SMarcel Telka 			p->rpos += xdrs->x_handy;
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 		m = m->b_cont;
2687c478bd9Sstevel@tonic-gate 		xdrs->x_base = (caddr_t)m;
269cf98b944SMarcel Telka 		p->apos += p->rpos;
270cf98b944SMarcel Telka 		p->rpos = 0;
2717c478bd9Sstevel@tonic-gate 		if (m == NULL) {
2727c478bd9Sstevel@tonic-gate 			xdrs->x_handy = 0;
2737c478bd9Sstevel@tonic-gate 			return (FALSE);
2747c478bd9Sstevel@tonic-gate 		}
275cf98b944SMarcel Telka 		xdrs->x_handy = (int)MBLKL(m);
2767c478bd9Sstevel@tonic-gate 	}
277cf98b944SMarcel Telka 
278cf98b944SMarcel Telka 	xdrs->x_handy -= len;
279cf98b944SMarcel Telka 	p->rpos += len;
280cf98b944SMarcel Telka 
2817c478bd9Sstevel@tonic-gate 	if (len < XDRMBLK_BCOPY_LIMIT) {
2827c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++)
2837c478bd9Sstevel@tonic-gate 			*addr++ = *m->b_rptr++;
2847c478bd9Sstevel@tonic-gate 	} else {
2857c478bd9Sstevel@tonic-gate 		bcopy(m->b_rptr, addr, len);
2867c478bd9Sstevel@tonic-gate 		m->b_rptr += len;
2877c478bd9Sstevel@tonic-gate 	}
288cf98b944SMarcel Telka 
2897c478bd9Sstevel@tonic-gate 	return (TRUE);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * Sort of like getbytes except that instead of getting bytes we return the
2947c478bd9Sstevel@tonic-gate  * mblk chain which contains the data.  If the data ends in the middle of
2957c478bd9Sstevel@tonic-gate  * an mblk, the mblk is dup'd and split, so that the data will end on an
2967c478bd9Sstevel@tonic-gate  * mblk.  Note that it is up to the caller to keep track of the data length
2977c478bd9Sstevel@tonic-gate  * and not walk too far down the mblk chain.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate bool_t
xdrmblk_getmblk(XDR * xdrs,mblk_t ** mm,uint_t * lenp)3017c478bd9Sstevel@tonic-gate xdrmblk_getmblk(XDR *xdrs, mblk_t **mm, uint_t *lenp)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	mblk_t *m, *nextm;
304cf98b944SMarcel Telka 	struct xdrmblk_params *p;
3057c478bd9Sstevel@tonic-gate 	int len;
306cf98b944SMarcel Telka 	uint32_t llen;
3077c478bd9Sstevel@tonic-gate 
308cf98b944SMarcel Telka 	if (!xdrmblk_getint32(xdrs, (int32_t *)&llen))
3097c478bd9Sstevel@tonic-gate 		return (FALSE);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	*lenp = llen;
3127c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
3137c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
3147c478bd9Sstevel@tonic-gate 	*mm = m;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/*
3177c478bd9Sstevel@tonic-gate 	 * Walk the mblk chain until we get to the end or we've gathered
3187c478bd9Sstevel@tonic-gate 	 * enough data.
3197c478bd9Sstevel@tonic-gate 	 */
3207c478bd9Sstevel@tonic-gate 	len = 0;
3217c478bd9Sstevel@tonic-gate 	llen = roundup(llen, BYTES_PER_XDR_UNIT);
3227c478bd9Sstevel@tonic-gate 	while (m != NULL && len + (int)MBLKL(m) <= llen) {
3237c478bd9Sstevel@tonic-gate 		len += (int)MBLKL(m);
3247c478bd9Sstevel@tonic-gate 		m = m->b_cont;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	if (len < llen) {
3277c478bd9Sstevel@tonic-gate 		if (m == NULL) {
3287c478bd9Sstevel@tonic-gate 			return (FALSE);
3297c478bd9Sstevel@tonic-gate 		} else {
3307c478bd9Sstevel@tonic-gate 			int tail_bytes = llen - len;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 			/*
3337c478bd9Sstevel@tonic-gate 			 * Split the mblk with the last chunk of data and
3347c478bd9Sstevel@tonic-gate 			 * insert it into the chain.  The new mblk goes
3357c478bd9Sstevel@tonic-gate 			 * after the existing one so that it will get freed
3367c478bd9Sstevel@tonic-gate 			 * properly.
3377c478bd9Sstevel@tonic-gate 			 */
3387c478bd9Sstevel@tonic-gate 			nextm = dupb(m);
3397c478bd9Sstevel@tonic-gate 			if (nextm == NULL)
3407c478bd9Sstevel@tonic-gate 				return (FALSE);
3417c478bd9Sstevel@tonic-gate 			nextm->b_cont = m->b_cont;
3427c478bd9Sstevel@tonic-gate 			m->b_cont = nextm;
3437c478bd9Sstevel@tonic-gate 			m->b_wptr = m->b_rptr + tail_bytes;
3447c478bd9Sstevel@tonic-gate 			nextm->b_rptr += tail_bytes;
3457c478bd9Sstevel@tonic-gate 			ASSERT(nextm->b_rptr != nextm->b_wptr);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 			m = nextm;	/* for x_base */
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	xdrs->x_base = (caddr_t)m;
3517c478bd9Sstevel@tonic-gate 	xdrs->x_handy = m != NULL ? MBLKL(m) : 0;
352cf98b944SMarcel Telka 
353cf98b944SMarcel Telka 	p = (struct xdrmblk_params *)xdrs->x_private;
354cf98b944SMarcel Telka 	p->apos += p->rpos + llen;
355cf98b944SMarcel Telka 	p->rpos = 0;
356cf98b944SMarcel Telka 
3577c478bd9Sstevel@tonic-gate 	return (TRUE);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_putbytes(XDR * xdrs,caddr_t addr,int len)3617c478bd9Sstevel@tonic-gate xdrmblk_putbytes(XDR *xdrs, caddr_t addr, int len)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	mblk_t *m;
364cf98b944SMarcel Telka 	struct xdrmblk_params *p;
365cf98b944SMarcel Telka 	int i;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
3687c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
3697c478bd9Sstevel@tonic-gate 	if (m == NULL)
3707c478bd9Sstevel@tonic-gate 		return (FALSE);
371cf98b944SMarcel Telka 
372cf98b944SMarcel Telka 	p = (struct xdrmblk_params *)xdrs->x_private;
373cf98b944SMarcel Telka 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * Performance tweak: converted explicit bcopy()
3767c478bd9Sstevel@tonic-gate 	 * call to simple in-line. This function is called
3777c478bd9Sstevel@tonic-gate 	 * to process things like readdir reply filenames
3787c478bd9Sstevel@tonic-gate 	 * which are small strings--typically 12 bytes or less.
3797c478bd9Sstevel@tonic-gate 	 * Overhead of calling bcopy() is obnoxious for such
3807c478bd9Sstevel@tonic-gate 	 * small copies.
3817c478bd9Sstevel@tonic-gate 	 */
382cf98b944SMarcel Telka 	while (xdrs->x_handy < len) {
383cf98b944SMarcel Telka 		if (xdrs->x_handy > 0) {
3847c478bd9Sstevel@tonic-gate 			if (xdrs->x_handy < XDRMBLK_BCOPY_LIMIT) {
385cf98b944SMarcel Telka 				for (i = 0; i < xdrs->x_handy; i++)
3867c478bd9Sstevel@tonic-gate 					*m->b_wptr++ = *addr++;
3877c478bd9Sstevel@tonic-gate 			} else {
3887c478bd9Sstevel@tonic-gate 				bcopy(addr, m->b_wptr, xdrs->x_handy);
3897c478bd9Sstevel@tonic-gate 				m->b_wptr += xdrs->x_handy;
3907c478bd9Sstevel@tonic-gate 				addr += xdrs->x_handy;
3917c478bd9Sstevel@tonic-gate 			}
3927c478bd9Sstevel@tonic-gate 			len -= xdrs->x_handy;
393cf98b944SMarcel Telka 			p->rpos += xdrs->x_handy;
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		/*
3977c478bd9Sstevel@tonic-gate 		 * We don't have enough space, so allocate the
398cf98b944SMarcel Telka 		 * amount we need, or sz, whichever is larger.
3997c478bd9Sstevel@tonic-gate 		 * It is better to let the underlying transport divide
4007c478bd9Sstevel@tonic-gate 		 * large chunks than to try and guess what is best.
4017c478bd9Sstevel@tonic-gate 		 */
4027c478bd9Sstevel@tonic-gate 		if (m->b_cont == NULL)
403cf98b944SMarcel Telka 			m->b_cont = xdrmblk_alloc(MAX(len, p->sz));
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 		m = m->b_cont;
4067c478bd9Sstevel@tonic-gate 		xdrs->x_base = (caddr_t)m;
407cf98b944SMarcel Telka 		p->apos += p->rpos;
408cf98b944SMarcel Telka 		p->rpos = 0;
4097c478bd9Sstevel@tonic-gate 		if (m == NULL) {
4107c478bd9Sstevel@tonic-gate 			xdrs->x_handy = 0;
4117c478bd9Sstevel@tonic-gate 			return (FALSE);
4127c478bd9Sstevel@tonic-gate 		}
413cf98b944SMarcel Telka 		xdrs->x_handy = (int)MBLKTAIL(m);
4147c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr == m->b_wptr);
4157c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr >= m->b_datap->db_base);
4167c478bd9Sstevel@tonic-gate 		ASSERT(m->b_rptr < m->b_datap->db_lim);
4177c478bd9Sstevel@tonic-gate 	}
418cf98b944SMarcel Telka 
419cf98b944SMarcel Telka 	xdrs->x_handy -= len;
420cf98b944SMarcel Telka 	p->rpos += len;
421cf98b944SMarcel Telka 
4227c478bd9Sstevel@tonic-gate 	if (len < XDRMBLK_BCOPY_LIMIT) {
4237c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++)
4247c478bd9Sstevel@tonic-gate 			*m->b_wptr++ = *addr++;
4257c478bd9Sstevel@tonic-gate 	} else {
4267c478bd9Sstevel@tonic-gate 		bcopy(addr, m->b_wptr, len);
4277c478bd9Sstevel@tonic-gate 		m->b_wptr += len;
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	ASSERT(m->b_wptr <= m->b_datap->db_lim);
4307c478bd9Sstevel@tonic-gate 	return (TRUE);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * We avoid a copy by merely adding this mblk to the list.  The caller is
4357c478bd9Sstevel@tonic-gate  * responsible for allocating and filling in the mblk. If len is
4367c478bd9Sstevel@tonic-gate  * not a multiple of BYTES_PER_XDR_UNIT, the caller has the option
4377c478bd9Sstevel@tonic-gate  * of making the data a BYTES_PER_XDR_UNIT multiple (b_wptr - b_rptr is
4387c478bd9Sstevel@tonic-gate  * a BYTES_PER_XDR_UNIT multiple), but in this case the caller has to ensure
439c242f9a0Schunli zhang - Sun Microsystems - Irvine United States  * that the filler bytes are initialized to zero.
4407c478bd9Sstevel@tonic-gate  */
4417c478bd9Sstevel@tonic-gate bool_t
xdrmblk_putmblk(XDR * xdrs,mblk_t * m,uint_t len)4427c478bd9Sstevel@tonic-gate xdrmblk_putmblk(XDR *xdrs, mblk_t *m, uint_t len)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	int32_t llen = (int32_t)len;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if (!xdrmblk_putint32(xdrs, &llen))
4477c478bd9Sstevel@tonic-gate 		return (FALSE);
448c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
449*5dde82e7SMarcel Telka 	return (xdrmblk_putmblk_raw(xdrs, m));
450*5dde82e7SMarcel Telka }
451*5dde82e7SMarcel Telka 
452*5dde82e7SMarcel Telka /*
453*5dde82e7SMarcel Telka  * The raw version of putmblk does not prepend the added data with the length.
454*5dde82e7SMarcel Telka  */
455*5dde82e7SMarcel Telka bool_t
xdrmblk_putmblk_raw(XDR * xdrs,mblk_t * m)456*5dde82e7SMarcel Telka xdrmblk_putmblk_raw(XDR *xdrs, mblk_t *m)
457*5dde82e7SMarcel Telka {
458*5dde82e7SMarcel Telka 	struct xdrmblk_params *p;
459*5dde82e7SMarcel Telka 
460*5dde82e7SMarcel Telka 	if ((DLEN(m) % BYTES_PER_XDR_UNIT) != 0)
461*5dde82e7SMarcel Telka 		return (FALSE);
462*5dde82e7SMarcel Telka 
463cf98b944SMarcel Telka 	p = (struct xdrmblk_params *)xdrs->x_private;
464cf98b944SMarcel Telka 
4657c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
4667c478bd9Sstevel@tonic-gate 	((mblk_t *)xdrs->x_base)->b_cont = m;
467cf98b944SMarcel Telka 	p->apos += p->rpos;
468c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 
469c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 	/* base points to the last mblk */
470cf98b944SMarcel Telka 	while (m->b_cont) {
471cf98b944SMarcel Telka 		p->apos += MBLKL(m);
472c242f9a0Schunli zhang - Sun Microsystems - Irvine United States 		m = m->b_cont;
473cf98b944SMarcel Telka 	}
4747c478bd9Sstevel@tonic-gate 	xdrs->x_base = (caddr_t)m;
4757c478bd9Sstevel@tonic-gate 	xdrs->x_handy = 0;
476cf98b944SMarcel Telka 	p->rpos = MBLKL(m);
4777c478bd9Sstevel@tonic-gate 	return (TRUE);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static uint_t
xdrmblk_getpos(XDR * xdrs)4817c478bd9Sstevel@tonic-gate xdrmblk_getpos(XDR *xdrs)
4827c478bd9Sstevel@tonic-gate {
483cf98b944SMarcel Telka 	struct xdrmblk_params *p = (struct xdrmblk_params *)xdrs->x_private;
4847c478bd9Sstevel@tonic-gate 
485cf98b944SMarcel Telka 	return (p->apos + p->rpos);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_setpos(XDR * xdrs,uint_t pos)4897c478bd9Sstevel@tonic-gate xdrmblk_setpos(XDR *xdrs, uint_t pos)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	mblk_t *m;
492cf98b944SMarcel Telka 	struct xdrmblk_params *p;
493cf98b944SMarcel Telka 
494cf98b944SMarcel Telka 	p = (struct xdrmblk_params *)xdrs->x_private;
495cf98b944SMarcel Telka 
496cf98b944SMarcel Telka 	if (pos < p->apos)
497cf98b944SMarcel Telka 		return (FALSE);
498cf98b944SMarcel Telka 
499cf98b944SMarcel Telka 	if (pos > p->apos + p->rpos + xdrs->x_handy)
500cf98b944SMarcel Telka 		return (FALSE);
501cf98b944SMarcel Telka 
502cf98b944SMarcel Telka 	if (pos == p->apos + p->rpos)
503cf98b944SMarcel Telka 		return (TRUE);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	/* LINTED pointer alignment */
5067c478bd9Sstevel@tonic-gate 	m = (mblk_t *)xdrs->x_base;
507cf98b944SMarcel Telka 	ASSERT(m != NULL);
5087c478bd9Sstevel@tonic-gate 
509cf98b944SMarcel Telka 	if (xdrs->x_op == XDR_DECODE)
510cf98b944SMarcel Telka 		m->b_rptr = m->b_rptr - p->rpos + (pos - p->apos);
511cf98b944SMarcel Telka 	else
512cf98b944SMarcel Telka 		m->b_wptr = m->b_wptr - p->rpos + (pos - p->apos);
5137c478bd9Sstevel@tonic-gate 
514cf98b944SMarcel Telka 	xdrs->x_handy = p->rpos + xdrs->x_handy - (pos - p->apos);
515cf98b944SMarcel Telka 	p->rpos = pos - p->apos;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	return (TRUE);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate #ifdef DEBUG
5217c478bd9Sstevel@tonic-gate static int xdrmblk_inline_hits = 0;
5227c478bd9Sstevel@tonic-gate static int xdrmblk_inline_misses = 0;
5237c478bd9Sstevel@tonic-gate static int do_xdrmblk_inline = 1;
5247c478bd9Sstevel@tonic-gate #endif
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate static rpc_inline_t *
xdrmblk_inline(XDR * xdrs,int len)5277c478bd9Sstevel@tonic-gate xdrmblk_inline(XDR *xdrs, int len)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	rpc_inline_t *buf;
5307c478bd9Sstevel@tonic-gate 	mblk_t *m;
531cf98b944SMarcel Telka 	unsigned char **mptr;
532cf98b944SMarcel Telka 	struct xdrmblk_params *p;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	/*
5357c478bd9Sstevel@tonic-gate 	 * Can't inline XDR_FREE calls, doesn't make sense.
5367c478bd9Sstevel@tonic-gate 	 */
5377c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_FREE)
5387c478bd9Sstevel@tonic-gate 		return (NULL);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate #ifdef DEBUG
541cf98b944SMarcel Telka 	if (!do_xdrmblk_inline) {
5427c478bd9Sstevel@tonic-gate 		xdrmblk_inline_misses++;
5437c478bd9Sstevel@tonic-gate 		return (NULL);
5447c478bd9Sstevel@tonic-gate 	}
545cf98b944SMarcel Telka #endif
546cf98b944SMarcel Telka 
547cf98b944SMarcel Telka 	if (xdrs->x_op == XDR_DECODE)
548cf98b944SMarcel Telka 		xdrmblk_skip_fully_read_mblks(xdrs);
5497c478bd9Sstevel@tonic-gate 
550cf98b944SMarcel Telka 	/*
551cf98b944SMarcel Telka 	 * Can't inline if there isn't enough room.
552cf98b944SMarcel Telka 	 */
553cf98b944SMarcel Telka 	if (len <= 0 || xdrs->x_handy < len) {
5547c478bd9Sstevel@tonic-gate #ifdef DEBUG
5557c478bd9Sstevel@tonic-gate 		xdrmblk_inline_misses++;
556cf98b944SMarcel Telka #endif
5577c478bd9Sstevel@tonic-gate 		return (NULL);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
560cf98b944SMarcel Telka 	/* LINTED pointer alignment */
561cf98b944SMarcel Telka 	m = (mblk_t *)xdrs->x_base;
562cf98b944SMarcel Telka 	ASSERT(m != NULL);
563cf98b944SMarcel Telka 
5647c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_DECODE) {
5657c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
566cf98b944SMarcel Telka 		mptr = &m->b_rptr;
5677c478bd9Sstevel@tonic-gate 	} else {
5687c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
569cf98b944SMarcel Telka 		mptr = &m->b_wptr;
5707c478bd9Sstevel@tonic-gate 	}
571cf98b944SMarcel Telka 
572cf98b944SMarcel Telka 	/*
573cf98b944SMarcel Telka 	 * Can't inline if the buffer is not 4 byte aligned, or if there is
574cf98b944SMarcel Telka 	 * more than one reference to the data block associated with this mblk.
575cf98b944SMarcel Telka 	 * This last check is used because the caller may want to modify the
576cf98b944SMarcel Telka 	 * data in the inlined portion and someone else is holding a reference
577cf98b944SMarcel Telka 	 * to the data who may not want it to be modified.
578cf98b944SMarcel Telka 	 */
579cf98b944SMarcel Telka 	if (!IS_P2ALIGNED(*mptr, sizeof (int32_t)) ||
580cf98b944SMarcel Telka 	    m->b_datap->db_ref != 1) {
581cf98b944SMarcel Telka #ifdef DEBUG
582cf98b944SMarcel Telka 		xdrmblk_inline_misses++;
583cf98b944SMarcel Telka #endif
584cf98b944SMarcel Telka 		return (NULL);
585cf98b944SMarcel Telka 	}
586cf98b944SMarcel Telka 
587cf98b944SMarcel Telka 	buf = (rpc_inline_t *)*mptr;
588cf98b944SMarcel Telka 
589cf98b944SMarcel Telka 	p = (struct xdrmblk_params *)xdrs->x_private;
590cf98b944SMarcel Telka 
591cf98b944SMarcel Telka 	*mptr += len;
592cf98b944SMarcel Telka 	xdrs->x_handy -= len;
593cf98b944SMarcel Telka 	p->rpos += len;
594cf98b944SMarcel Telka 
5957c478bd9Sstevel@tonic-gate #ifdef DEBUG
5967c478bd9Sstevel@tonic-gate 	xdrmblk_inline_hits++;
5977c478bd9Sstevel@tonic-gate #endif
598cf98b944SMarcel Telka 
5997c478bd9Sstevel@tonic-gate 	return (buf);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate static bool_t
xdrmblk_control(XDR * xdrs,int request,void * info)6037c478bd9Sstevel@tonic-gate xdrmblk_control(XDR *xdrs, int request, void *info)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	mblk_t *m;
606cf98b944SMarcel Telka 	struct xdrmblk_params *p;
6077c478bd9Sstevel@tonic-gate 	int32_t *int32p;
6087c478bd9Sstevel@tonic-gate 	int len;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	switch (request) {
6117c478bd9Sstevel@tonic-gate 	case XDR_PEEK:
612cf98b944SMarcel Telka 		xdrmblk_skip_fully_read_mblks(xdrs);
613cf98b944SMarcel Telka 
6147c478bd9Sstevel@tonic-gate 		/*
6157c478bd9Sstevel@tonic-gate 		 * Return the next 4 byte unit in the XDR stream.
6167c478bd9Sstevel@tonic-gate 		 */
6177c478bd9Sstevel@tonic-gate 		if (xdrs->x_handy < sizeof (int32_t))
6187c478bd9Sstevel@tonic-gate 			return (FALSE);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
6217c478bd9Sstevel@tonic-gate 		m = (mblk_t *)xdrs->x_base;
622cf98b944SMarcel Telka 		ASSERT(m != NULL);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		/*
6257c478bd9Sstevel@tonic-gate 		 * If the pointer is not aligned, fail the peek
6267c478bd9Sstevel@tonic-gate 		 */
6277c478bd9Sstevel@tonic-gate 		if (!IS_P2ALIGNED(m->b_rptr, sizeof (int32_t)))
6287c478bd9Sstevel@tonic-gate 			return (FALSE);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		int32p = (int32_t *)info;
6317c478bd9Sstevel@tonic-gate 		/* LINTED pointer alignment */
6327c478bd9Sstevel@tonic-gate 		*int32p = ntohl(*((int32_t *)(m->b_rptr)));
6337c478bd9Sstevel@tonic-gate 		return (TRUE);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	case XDR_SKIPBYTES:
6367c478bd9Sstevel@tonic-gate 		int32p = (int32_t *)info;
6377c478bd9Sstevel@tonic-gate 		len = RNDUP((int)(*int32p));
638742e7b7fSmaheshvs 		if (len < 0)
6390a701b1eSRobert Gordon 			return (FALSE);
640cf98b944SMarcel Telka 		if (len == 0)
641cf98b944SMarcel Telka 			return (TRUE);
642cf98b944SMarcel Telka 
643cf98b944SMarcel Telka 		/* LINTED pointer alignment */
644cf98b944SMarcel Telka 		m = (mblk_t *)xdrs->x_base;
645cf98b944SMarcel Telka 		if (m == NULL)
646cf98b944SMarcel Telka 			return (FALSE);
647cf98b944SMarcel Telka 
648cf98b944SMarcel Telka 		p = (struct xdrmblk_params *)xdrs->x_private;
649cf98b944SMarcel Telka 
650cf98b944SMarcel Telka 		while (xdrs->x_handy < len) {
651cf98b944SMarcel Telka 			if (xdrs->x_handy > 0) {
6527c478bd9Sstevel@tonic-gate 				m->b_rptr += xdrs->x_handy;
6537c478bd9Sstevel@tonic-gate 				len -= xdrs->x_handy;
654cf98b944SMarcel Telka 				p->rpos += xdrs->x_handy;
6557c478bd9Sstevel@tonic-gate 			}
6567c478bd9Sstevel@tonic-gate 			m = m->b_cont;
6577c478bd9Sstevel@tonic-gate 			xdrs->x_base = (caddr_t)m;
658cf98b944SMarcel Telka 			p->apos += p->rpos;
659cf98b944SMarcel Telka 			p->rpos = 0;
6607c478bd9Sstevel@tonic-gate 			if (m == NULL) {
6617c478bd9Sstevel@tonic-gate 				xdrs->x_handy = 0;
6627c478bd9Sstevel@tonic-gate 				return (FALSE);
6637c478bd9Sstevel@tonic-gate 			}
664cf98b944SMarcel Telka 			xdrs->x_handy = (int)MBLKL(m);
6657c478bd9Sstevel@tonic-gate 		}
666cf98b944SMarcel Telka 
667cf98b944SMarcel Telka 		xdrs->x_handy -= len;
668cf98b944SMarcel Telka 		p->rpos += len;
6697c478bd9Sstevel@tonic-gate 		m->b_rptr += len;
6707c478bd9Sstevel@tonic-gate 		return (TRUE);
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	default:
6737c478bd9Sstevel@tonic-gate 		return (FALSE);
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate #define	HDR_SPACE	128
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate static mblk_t *
xdrmblk_alloc(int sz)6807c478bd9Sstevel@tonic-gate xdrmblk_alloc(int sz)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	mblk_t *mp;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (sz == 0)
6857c478bd9Sstevel@tonic-gate 		return (NULL);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/*
6887c478bd9Sstevel@tonic-gate 	 * Pad the front of the message to allow the lower networking
6897c478bd9Sstevel@tonic-gate 	 * layers space to add headers as needed.
6907c478bd9Sstevel@tonic-gate 	 */
6917c478bd9Sstevel@tonic-gate 	sz += HDR_SPACE;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	while ((mp = allocb(sz, BPRI_LO)) == NULL) {
6947c478bd9Sstevel@tonic-gate 		if (strwaitbuf(sz, BPRI_LO))
6957c478bd9Sstevel@tonic-gate 			return (NULL);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	mp->b_wptr += HDR_SPACE;
6997c478bd9Sstevel@tonic-gate 	mp->b_rptr = mp->b_wptr;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	return (mp);
7027c478bd9Sstevel@tonic-gate }
703cf98b944SMarcel Telka 
704cf98b944SMarcel Telka /*
705cf98b944SMarcel Telka  * Skip fully read or empty mblks
706cf98b944SMarcel Telka  */
707cf98b944SMarcel Telka static void
xdrmblk_skip_fully_read_mblks(XDR * xdrs)708cf98b944SMarcel Telka xdrmblk_skip_fully_read_mblks(XDR *xdrs)
709cf98b944SMarcel Telka {
710cf98b944SMarcel Telka 	mblk_t *m;
711cf98b944SMarcel Telka 	struct xdrmblk_params *p;
712cf98b944SMarcel Telka 
713cf98b944SMarcel Telka 	if (xdrs->x_handy != 0)
714cf98b944SMarcel Telka 		return;
715cf98b944SMarcel Telka 
716cf98b944SMarcel Telka 	/* LINTED pointer alignment */
717cf98b944SMarcel Telka 	m = (mblk_t *)xdrs->x_base;
718cf98b944SMarcel Telka 	if (m == NULL)
719cf98b944SMarcel Telka 		return;
720cf98b944SMarcel Telka 
721cf98b944SMarcel Telka 	p = (struct xdrmblk_params *)xdrs->x_private;
722cf98b944SMarcel Telka 	p->apos += p->rpos;
723cf98b944SMarcel Telka 	p->rpos = 0;
724cf98b944SMarcel Telka 
725cf98b944SMarcel Telka 	do {
726cf98b944SMarcel Telka 		m = m->b_cont;
727cf98b944SMarcel Telka 		if (m == NULL)
728cf98b944SMarcel Telka 			break;
729cf98b944SMarcel Telka 
730cf98b944SMarcel Telka 		xdrs->x_handy = (int)MBLKL(m);
731cf98b944SMarcel Telka 	} while (xdrs->x_handy == 0);
732cf98b944SMarcel Telka 
733cf98b944SMarcel Telka 	xdrs->x_base = (caddr_t)m;
734cf98b944SMarcel Telka }
735