19454b2d8SWarner Losh /*-
2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni *
4d8589bd5SBoris Popov * Copyright (c) 2000, 2001 Boris Popov
5d8589bd5SBoris Popov * All rights reserved.
6d8589bd5SBoris Popov *
7d8589bd5SBoris Popov * Redistribution and use in source and binary forms, with or without
8d8589bd5SBoris Popov * modification, are permitted provided that the following conditions
9d8589bd5SBoris Popov * are met:
10d8589bd5SBoris Popov * 1. Redistributions of source code must retain the above copyright
11d8589bd5SBoris Popov * notice, this list of conditions and the following disclaimer.
12d8589bd5SBoris Popov * 2. Redistributions in binary form must reproduce the above copyright
13d8589bd5SBoris Popov * notice, this list of conditions and the following disclaimer in the
14d8589bd5SBoris Popov * documentation and/or other materials provided with the distribution.
1569a28758SEd Maste * 3. Neither the name of the author nor the names of any co-contributors
16d8589bd5SBoris Popov * may be used to endorse or promote products derived from this software
17d8589bd5SBoris Popov * without specific prior written permission.
18d8589bd5SBoris Popov *
19d8589bd5SBoris Popov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20d8589bd5SBoris Popov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21d8589bd5SBoris Popov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22d8589bd5SBoris Popov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23d8589bd5SBoris Popov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24d8589bd5SBoris Popov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25d8589bd5SBoris Popov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26d8589bd5SBoris Popov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27d8589bd5SBoris Popov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28d8589bd5SBoris Popov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29d8589bd5SBoris Popov * SUCH DAMAGE.
30d8589bd5SBoris Popov */
31d8589bd5SBoris Popov
32d8589bd5SBoris Popov #include <sys/param.h>
33d8589bd5SBoris Popov #include <sys/systm.h>
34de5b1952SAlexander Leidinger #include <sys/sysctl.h>
35a30d4b32SMike Barcroft #include <sys/endian.h>
36d8589bd5SBoris Popov #include <sys/errno.h>
378ec07310SGleb Smirnoff #include <sys/malloc.h>
38d8589bd5SBoris Popov #include <sys/mbuf.h>
39d8589bd5SBoris Popov #include <sys/module.h>
40d8589bd5SBoris Popov #include <sys/uio.h>
41d8589bd5SBoris Popov
42d8589bd5SBoris Popov #include <sys/mchain.h>
43d8589bd5SBoris Popov
44de5b1952SAlexander Leidinger FEATURE(libmchain, "mchain library");
45de5b1952SAlexander Leidinger
46d8589bd5SBoris Popov MODULE_VERSION(libmchain, 1);
47d8589bd5SBoris Popov
488deedb62SMark Murray #define MBERROR(format, ...) printf("%s(%d): "format, __func__ , \
498deedb62SMark Murray __LINE__ , ## __VA_ARGS__)
50d8589bd5SBoris Popov
518deedb62SMark Murray #define MBPANIC(format, ...) printf("%s(%d): "format, __func__ , \
528deedb62SMark Murray __LINE__ , ## __VA_ARGS__)
53d8589bd5SBoris Popov
54d8589bd5SBoris Popov /*
55d8589bd5SBoris Popov * Various helper functions
56d8589bd5SBoris Popov */
57d8589bd5SBoris Popov int
mb_init(struct mbchain * mbp)58d8589bd5SBoris Popov mb_init(struct mbchain *mbp)
59d8589bd5SBoris Popov {
60d8589bd5SBoris Popov struct mbuf *m;
61d8589bd5SBoris Popov
62eb1b1807SGleb Smirnoff m = m_gethdr(M_WAITOK, MT_DATA);
63d8589bd5SBoris Popov m->m_len = 0;
64d8589bd5SBoris Popov mb_initm(mbp, m);
650ca2381dSKevin Lo return (0);
66d8589bd5SBoris Popov }
67d8589bd5SBoris Popov
68d8589bd5SBoris Popov void
mb_initm(struct mbchain * mbp,struct mbuf * m)69d8589bd5SBoris Popov mb_initm(struct mbchain *mbp, struct mbuf *m)
70d8589bd5SBoris Popov {
71d8589bd5SBoris Popov bzero(mbp, sizeof(*mbp));
72d8589bd5SBoris Popov mbp->mb_top = mbp->mb_cur = m;
73d8589bd5SBoris Popov mbp->mb_mleft = M_TRAILINGSPACE(m);
74d8589bd5SBoris Popov }
75d8589bd5SBoris Popov
76d8589bd5SBoris Popov void
mb_done(struct mbchain * mbp)77d8589bd5SBoris Popov mb_done(struct mbchain *mbp)
78d8589bd5SBoris Popov {
79d8589bd5SBoris Popov if (mbp->mb_top) {
80d8589bd5SBoris Popov m_freem(mbp->mb_top);
81d8589bd5SBoris Popov mbp->mb_top = NULL;
82d8589bd5SBoris Popov }
83d8589bd5SBoris Popov }
84d8589bd5SBoris Popov
85d8589bd5SBoris Popov struct mbuf *
mb_detach(struct mbchain * mbp)86d8589bd5SBoris Popov mb_detach(struct mbchain *mbp)
87d8589bd5SBoris Popov {
88d8589bd5SBoris Popov struct mbuf *m;
89d8589bd5SBoris Popov
90d8589bd5SBoris Popov m = mbp->mb_top;
91d8589bd5SBoris Popov mbp->mb_top = NULL;
920ca2381dSKevin Lo return (m);
93d8589bd5SBoris Popov }
94d8589bd5SBoris Popov
95d8589bd5SBoris Popov int
mb_fixhdr(struct mbchain * mbp)96d8589bd5SBoris Popov mb_fixhdr(struct mbchain *mbp)
97d8589bd5SBoris Popov {
980ca2381dSKevin Lo return (mbp->mb_top->m_pkthdr.len = m_fixhdr(mbp->mb_top));
99d8589bd5SBoris Popov }
100d8589bd5SBoris Popov
101d8589bd5SBoris Popov /*
102d8589bd5SBoris Popov * Check if object of size 'size' fit to the current position and
103d8589bd5SBoris Popov * allocate new mbuf if not. Advance pointers and increase length of mbuf(s).
104e3043798SPedro F. Giffuni * Return pointer to the object placeholder or NULL if any error occurred.
105d8589bd5SBoris Popov * Note: size should be <= MLEN
106d8589bd5SBoris Popov */
107d8589bd5SBoris Popov caddr_t
mb_reserve(struct mbchain * mbp,int size)108d8589bd5SBoris Popov mb_reserve(struct mbchain *mbp, int size)
109d8589bd5SBoris Popov {
110d8589bd5SBoris Popov struct mbuf *m, *mn;
111d8589bd5SBoris Popov caddr_t bpos;
112d8589bd5SBoris Popov
113d8589bd5SBoris Popov if (size > MLEN)
114d8589bd5SBoris Popov panic("mb_reserve: size = %d\n", size);
115d8589bd5SBoris Popov m = mbp->mb_cur;
116d8589bd5SBoris Popov if (mbp->mb_mleft < size) {
117eb1b1807SGleb Smirnoff mn = m_get(M_WAITOK, MT_DATA);
118d8589bd5SBoris Popov mbp->mb_cur = m->m_next = mn;
119d8589bd5SBoris Popov m = mn;
120d8589bd5SBoris Popov m->m_len = 0;
121d8589bd5SBoris Popov mbp->mb_mleft = M_TRAILINGSPACE(m);
122d8589bd5SBoris Popov }
123d8589bd5SBoris Popov mbp->mb_mleft -= size;
124d8589bd5SBoris Popov mbp->mb_count += size;
125d8589bd5SBoris Popov bpos = mtod(m, caddr_t) + m->m_len;
126d8589bd5SBoris Popov m->m_len += size;
1270ca2381dSKevin Lo return (bpos);
128d8589bd5SBoris Popov }
129d8589bd5SBoris Popov
130d8589bd5SBoris Popov int
mb_put_padbyte(struct mbchain * mbp)13141f1dcccSKevin Lo mb_put_padbyte(struct mbchain *mbp)
13241f1dcccSKevin Lo {
13341f1dcccSKevin Lo caddr_t dst;
1340ca2381dSKevin Lo uint8_t x = 0;
13541f1dcccSKevin Lo
13641f1dcccSKevin Lo dst = mtod(mbp->mb_cur, caddr_t) + mbp->mb_cur->m_len;
13741f1dcccSKevin Lo
1380ca2381dSKevin Lo /* Only add padding if address is odd */
13941f1dcccSKevin Lo if ((unsigned long)dst & 1)
1400ca2381dSKevin Lo return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
14141f1dcccSKevin Lo else
1420ca2381dSKevin Lo return (0);
14341f1dcccSKevin Lo }
14441f1dcccSKevin Lo
14541f1dcccSKevin Lo int
mb_put_uint8(struct mbchain * mbp,uint8_t x)14660ae52f7SEd Schouten mb_put_uint8(struct mbchain *mbp, uint8_t x)
147d8589bd5SBoris Popov {
1480ca2381dSKevin Lo return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
149d8589bd5SBoris Popov }
150d8589bd5SBoris Popov
151d8589bd5SBoris Popov int
mb_put_uint16be(struct mbchain * mbp,uint16_t x)15260ae52f7SEd Schouten mb_put_uint16be(struct mbchain *mbp, uint16_t x)
153d8589bd5SBoris Popov {
1540adb6d7aSRobert Drehmel x = htobe16(x);
1550ca2381dSKevin Lo return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
156d8589bd5SBoris Popov }
157d8589bd5SBoris Popov
158d8589bd5SBoris Popov int
mb_put_uint16le(struct mbchain * mbp,uint16_t x)15960ae52f7SEd Schouten mb_put_uint16le(struct mbchain *mbp, uint16_t x)
160d8589bd5SBoris Popov {
1610adb6d7aSRobert Drehmel x = htole16(x);
1620ca2381dSKevin Lo return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
163d8589bd5SBoris Popov }
164d8589bd5SBoris Popov
165d8589bd5SBoris Popov int
mb_put_uint32be(struct mbchain * mbp,uint32_t x)16660ae52f7SEd Schouten mb_put_uint32be(struct mbchain *mbp, uint32_t x)
167d8589bd5SBoris Popov {
1680adb6d7aSRobert Drehmel x = htobe32(x);
1690ca2381dSKevin Lo return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
170d8589bd5SBoris Popov }
171d8589bd5SBoris Popov
172d8589bd5SBoris Popov int
mb_put_uint32le(struct mbchain * mbp,uint32_t x)17360ae52f7SEd Schouten mb_put_uint32le(struct mbchain *mbp, uint32_t x)
174d8589bd5SBoris Popov {
1750adb6d7aSRobert Drehmel x = htole32(x);
1760ca2381dSKevin Lo return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
177d8589bd5SBoris Popov }
178d8589bd5SBoris Popov
179d8589bd5SBoris Popov int
mb_put_int64be(struct mbchain * mbp,int64_t x)180d8589bd5SBoris Popov mb_put_int64be(struct mbchain *mbp, int64_t x)
181d8589bd5SBoris Popov {
1820adb6d7aSRobert Drehmel x = htobe64(x);
1830ca2381dSKevin Lo return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
184d8589bd5SBoris Popov }
185d8589bd5SBoris Popov
186d8589bd5SBoris Popov int
mb_put_int64le(struct mbchain * mbp,int64_t x)187d8589bd5SBoris Popov mb_put_int64le(struct mbchain *mbp, int64_t x)
188d8589bd5SBoris Popov {
1890adb6d7aSRobert Drehmel x = htole64(x);
1900ca2381dSKevin Lo return (mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM));
191d8589bd5SBoris Popov }
192d8589bd5SBoris Popov
193d8589bd5SBoris Popov int
mb_put_mem(struct mbchain * mbp,c_caddr_t source,int size,int type)194d8589bd5SBoris Popov mb_put_mem(struct mbchain *mbp, c_caddr_t source, int size, int type)
195d8589bd5SBoris Popov {
196d8589bd5SBoris Popov struct mbuf *m;
197d8589bd5SBoris Popov caddr_t dst;
198d8589bd5SBoris Popov c_caddr_t src;
199d8589bd5SBoris Popov int cplen, error, mleft, count;
200080e3a63SR. Imura size_t srclen, dstlen;
201d8589bd5SBoris Popov
202d8589bd5SBoris Popov m = mbp->mb_cur;
203d8589bd5SBoris Popov mleft = mbp->mb_mleft;
204d8589bd5SBoris Popov
205d8589bd5SBoris Popov while (size > 0) {
206d8589bd5SBoris Popov if (mleft == 0) {
207ea26d587SRuslan Ermilov if (m->m_next == NULL)
208eb1b1807SGleb Smirnoff m = m_getm(m, size, M_WAITOK, MT_DATA);
209ea26d587SRuslan Ermilov else
210a44a414eSTim J. Robbins m = m->m_next;
211d8589bd5SBoris Popov mleft = M_TRAILINGSPACE(m);
212d8589bd5SBoris Popov continue;
213d8589bd5SBoris Popov }
214d8589bd5SBoris Popov cplen = mleft > size ? size : mleft;
215080e3a63SR. Imura srclen = dstlen = cplen;
216d8589bd5SBoris Popov dst = mtod(m, caddr_t) + m->m_len;
217d8589bd5SBoris Popov switch (type) {
218d8589bd5SBoris Popov case MB_MCUSTOM:
219080e3a63SR. Imura srclen = size;
220080e3a63SR. Imura dstlen = mleft;
221080e3a63SR. Imura error = mbp->mb_copy(mbp, source, dst, &srclen, &dstlen);
222d8589bd5SBoris Popov if (error)
2230ca2381dSKevin Lo return (error);
224d8589bd5SBoris Popov break;
225d8589bd5SBoris Popov case MB_MINLINE:
226d8589bd5SBoris Popov for (src = source, count = cplen; count; count--)
227d8589bd5SBoris Popov *dst++ = *src++;
228d8589bd5SBoris Popov break;
229d8589bd5SBoris Popov case MB_MSYSTEM:
230d8589bd5SBoris Popov bcopy(source, dst, cplen);
231d8589bd5SBoris Popov break;
232d8589bd5SBoris Popov case MB_MUSER:
233d8589bd5SBoris Popov error = copyin(source, dst, cplen);
234d8589bd5SBoris Popov if (error)
2350ca2381dSKevin Lo return (error);
236d8589bd5SBoris Popov break;
237d8589bd5SBoris Popov case MB_MZERO:
238d8589bd5SBoris Popov bzero(dst, cplen);
239d8589bd5SBoris Popov break;
240d8589bd5SBoris Popov }
241080e3a63SR. Imura size -= srclen;
242080e3a63SR. Imura source += srclen;
243080e3a63SR. Imura m->m_len += dstlen;
244080e3a63SR. Imura mleft -= dstlen;
245080e3a63SR. Imura mbp->mb_count += dstlen;
246d8589bd5SBoris Popov }
247d8589bd5SBoris Popov mbp->mb_cur = m;
248d8589bd5SBoris Popov mbp->mb_mleft = mleft;
2490ca2381dSKevin Lo return (0);
250d8589bd5SBoris Popov }
251d8589bd5SBoris Popov
252d8589bd5SBoris Popov int
mb_put_mbuf(struct mbchain * mbp,struct mbuf * m)253d8589bd5SBoris Popov mb_put_mbuf(struct mbchain *mbp, struct mbuf *m)
254d8589bd5SBoris Popov {
255d8589bd5SBoris Popov mbp->mb_cur->m_next = m;
256d8589bd5SBoris Popov while (m) {
257d8589bd5SBoris Popov mbp->mb_count += m->m_len;
258d8589bd5SBoris Popov if (m->m_next == NULL)
259d8589bd5SBoris Popov break;
260d8589bd5SBoris Popov m = m->m_next;
261d8589bd5SBoris Popov }
262d8589bd5SBoris Popov mbp->mb_mleft = M_TRAILINGSPACE(m);
263d8589bd5SBoris Popov mbp->mb_cur = m;
2640ca2381dSKevin Lo return (0);
265d8589bd5SBoris Popov }
266d8589bd5SBoris Popov
267d8589bd5SBoris Popov /*
268d8589bd5SBoris Popov * copies a uio scatter/gather list to an mbuf chain.
269d8589bd5SBoris Popov */
270d8589bd5SBoris Popov int
mb_put_uio(struct mbchain * mbp,struct uio * uiop,int size)271d8589bd5SBoris Popov mb_put_uio(struct mbchain *mbp, struct uio *uiop, int size)
272d8589bd5SBoris Popov {
273d8589bd5SBoris Popov long left;
274d8589bd5SBoris Popov int mtype, error;
275d8589bd5SBoris Popov
276d8589bd5SBoris Popov mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
277d8589bd5SBoris Popov
278cebcee2eSBoris Popov while (size > 0 && uiop->uio_resid) {
279cebcee2eSBoris Popov if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
2800ca2381dSKevin Lo return (EFBIG);
281d8589bd5SBoris Popov left = uiop->uio_iov->iov_len;
282cebcee2eSBoris Popov if (left == 0) {
283cebcee2eSBoris Popov uiop->uio_iov++;
284cebcee2eSBoris Popov uiop->uio_iovcnt--;
285cebcee2eSBoris Popov continue;
286cebcee2eSBoris Popov }
287d8589bd5SBoris Popov if (left > size)
288d8589bd5SBoris Popov left = size;
289d8589bd5SBoris Popov error = mb_put_mem(mbp, uiop->uio_iov->iov_base, left, mtype);
290d8589bd5SBoris Popov if (error)
2910ca2381dSKevin Lo return (error);
292d8589bd5SBoris Popov uiop->uio_offset += left;
293d8589bd5SBoris Popov uiop->uio_resid -= left;
2942b7f24d2SMike Barcroft uiop->uio_iov->iov_base =
2952b7f24d2SMike Barcroft (char *)uiop->uio_iov->iov_base + left;
296d8589bd5SBoris Popov uiop->uio_iov->iov_len -= left;
297d8589bd5SBoris Popov size -= left;
298d8589bd5SBoris Popov }
2990ca2381dSKevin Lo return (0);
300d8589bd5SBoris Popov }
301d8589bd5SBoris Popov
302d8589bd5SBoris Popov /*
303d8589bd5SBoris Popov * Routines for fetching data from an mbuf chain
304d8589bd5SBoris Popov */
305d8589bd5SBoris Popov int
md_init(struct mdchain * mdp)306d8589bd5SBoris Popov md_init(struct mdchain *mdp)
307d8589bd5SBoris Popov {
308d8589bd5SBoris Popov struct mbuf *m;
309d8589bd5SBoris Popov
310eb1b1807SGleb Smirnoff m = m_gethdr(M_WAITOK, MT_DATA);
311d8589bd5SBoris Popov m->m_len = 0;
312d8589bd5SBoris Popov md_initm(mdp, m);
3130ca2381dSKevin Lo return (0);
314d8589bd5SBoris Popov }
315d8589bd5SBoris Popov
316d8589bd5SBoris Popov void
md_initm(struct mdchain * mdp,struct mbuf * m)317d8589bd5SBoris Popov md_initm(struct mdchain *mdp, struct mbuf *m)
318d8589bd5SBoris Popov {
319d8589bd5SBoris Popov bzero(mdp, sizeof(*mdp));
320d8589bd5SBoris Popov mdp->md_top = mdp->md_cur = m;
321d8589bd5SBoris Popov mdp->md_pos = mtod(m, u_char*);
322d8589bd5SBoris Popov }
323d8589bd5SBoris Popov
324d8589bd5SBoris Popov void
md_done(struct mdchain * mdp)325d8589bd5SBoris Popov md_done(struct mdchain *mdp)
326d8589bd5SBoris Popov {
327d8589bd5SBoris Popov if (mdp->md_top) {
328d8589bd5SBoris Popov m_freem(mdp->md_top);
329d8589bd5SBoris Popov mdp->md_top = NULL;
330d8589bd5SBoris Popov }
331d8589bd5SBoris Popov }
332d8589bd5SBoris Popov
333d8589bd5SBoris Popov /*
334d8589bd5SBoris Popov * Append a separate mbuf chain. It is caller responsibility to prevent
335d8589bd5SBoris Popov * multiple calls to fetch/record routines.
336d8589bd5SBoris Popov */
337d8589bd5SBoris Popov void
md_append_record(struct mdchain * mdp,struct mbuf * top)338d8589bd5SBoris Popov md_append_record(struct mdchain *mdp, struct mbuf *top)
339d8589bd5SBoris Popov {
340d8589bd5SBoris Popov struct mbuf *m;
341d8589bd5SBoris Popov
342d8589bd5SBoris Popov if (mdp->md_top == NULL) {
343d8589bd5SBoris Popov md_initm(mdp, top);
344d8589bd5SBoris Popov return;
345d8589bd5SBoris Popov }
346d8589bd5SBoris Popov m = mdp->md_top;
347d8589bd5SBoris Popov while (m->m_nextpkt)
348d8589bd5SBoris Popov m = m->m_nextpkt;
349d8589bd5SBoris Popov m->m_nextpkt = top;
350d8589bd5SBoris Popov top->m_nextpkt = NULL;
351d8589bd5SBoris Popov return;
352d8589bd5SBoris Popov }
353d8589bd5SBoris Popov
354d8589bd5SBoris Popov /*
355d8589bd5SBoris Popov * Put next record in place of existing
356d8589bd5SBoris Popov */
357d8589bd5SBoris Popov int
md_next_record(struct mdchain * mdp)358d8589bd5SBoris Popov md_next_record(struct mdchain *mdp)
359d8589bd5SBoris Popov {
360d8589bd5SBoris Popov struct mbuf *m;
361d8589bd5SBoris Popov
362d8589bd5SBoris Popov if (mdp->md_top == NULL)
3630ca2381dSKevin Lo return (ENOENT);
364d8589bd5SBoris Popov m = mdp->md_top->m_nextpkt;
365d8589bd5SBoris Popov md_done(mdp);
366d8589bd5SBoris Popov if (m == NULL)
3670ca2381dSKevin Lo return (ENOENT);
368d8589bd5SBoris Popov md_initm(mdp, m);
3690ca2381dSKevin Lo return (0);
370d8589bd5SBoris Popov }
371d8589bd5SBoris Popov
372d8589bd5SBoris Popov int
md_get_uint8(struct mdchain * mdp,uint8_t * x)37360ae52f7SEd Schouten md_get_uint8(struct mdchain *mdp, uint8_t *x)
374d8589bd5SBoris Popov {
3750ca2381dSKevin Lo return (md_get_mem(mdp, x, 1, MB_MINLINE));
376d8589bd5SBoris Popov }
377d8589bd5SBoris Popov
378d8589bd5SBoris Popov int
md_get_uint16(struct mdchain * mdp,uint16_t * x)37960ae52f7SEd Schouten md_get_uint16(struct mdchain *mdp, uint16_t *x)
380d8589bd5SBoris Popov {
3810ca2381dSKevin Lo return (md_get_mem(mdp, (caddr_t)x, 2, MB_MINLINE));
382d8589bd5SBoris Popov }
383d8589bd5SBoris Popov
384d8589bd5SBoris Popov int
md_get_uint16le(struct mdchain * mdp,uint16_t * x)38560ae52f7SEd Schouten md_get_uint16le(struct mdchain *mdp, uint16_t *x)
386d8589bd5SBoris Popov {
38760ae52f7SEd Schouten uint16_t v;
388d8589bd5SBoris Popov int error = md_get_uint16(mdp, &v);
389d8589bd5SBoris Popov
39012f65109SJohn Baldwin if (x != NULL)
3910adb6d7aSRobert Drehmel *x = le16toh(v);
3920ca2381dSKevin Lo return (error);
393d8589bd5SBoris Popov }
394d8589bd5SBoris Popov
395d8589bd5SBoris Popov int
md_get_uint16be(struct mdchain * mdp,uint16_t * x)39660ae52f7SEd Schouten md_get_uint16be(struct mdchain *mdp, uint16_t *x)
39760ae52f7SEd Schouten {
39860ae52f7SEd Schouten uint16_t v;
399d8589bd5SBoris Popov int error = md_get_uint16(mdp, &v);
400d8589bd5SBoris Popov
40112f65109SJohn Baldwin if (x != NULL)
4020adb6d7aSRobert Drehmel *x = be16toh(v);
4030ca2381dSKevin Lo return (error);
404d8589bd5SBoris Popov }
405d8589bd5SBoris Popov
406d8589bd5SBoris Popov int
md_get_uint32(struct mdchain * mdp,uint32_t * x)40760ae52f7SEd Schouten md_get_uint32(struct mdchain *mdp, uint32_t *x)
408d8589bd5SBoris Popov {
4090ca2381dSKevin Lo return (md_get_mem(mdp, (caddr_t)x, 4, MB_MINLINE));
410d8589bd5SBoris Popov }
411d8589bd5SBoris Popov
412d8589bd5SBoris Popov int
md_get_uint32be(struct mdchain * mdp,uint32_t * x)41360ae52f7SEd Schouten md_get_uint32be(struct mdchain *mdp, uint32_t *x)
414d8589bd5SBoris Popov {
41560ae52f7SEd Schouten uint32_t v;
416d8589bd5SBoris Popov int error;
417d8589bd5SBoris Popov
418d8589bd5SBoris Popov error = md_get_uint32(mdp, &v);
41912f65109SJohn Baldwin if (x != NULL)
4200adb6d7aSRobert Drehmel *x = be32toh(v);
4210ca2381dSKevin Lo return (error);
422d8589bd5SBoris Popov }
423d8589bd5SBoris Popov
424d8589bd5SBoris Popov int
md_get_uint32le(struct mdchain * mdp,uint32_t * x)42560ae52f7SEd Schouten md_get_uint32le(struct mdchain *mdp, uint32_t *x)
426d8589bd5SBoris Popov {
42760ae52f7SEd Schouten uint32_t v;
428d8589bd5SBoris Popov int error;
429d8589bd5SBoris Popov
430d8589bd5SBoris Popov error = md_get_uint32(mdp, &v);
43112f65109SJohn Baldwin if (x != NULL)
4320adb6d7aSRobert Drehmel *x = le32toh(v);
4330ca2381dSKevin Lo return (error);
434d8589bd5SBoris Popov }
435d8589bd5SBoris Popov
436d8589bd5SBoris Popov int
md_get_int64(struct mdchain * mdp,int64_t * x)437d8589bd5SBoris Popov md_get_int64(struct mdchain *mdp, int64_t *x)
438d8589bd5SBoris Popov {
4390ca2381dSKevin Lo return (md_get_mem(mdp, (caddr_t)x, 8, MB_MINLINE));
440d8589bd5SBoris Popov }
441d8589bd5SBoris Popov
442d8589bd5SBoris Popov int
md_get_int64be(struct mdchain * mdp,int64_t * x)443d8589bd5SBoris Popov md_get_int64be(struct mdchain *mdp, int64_t *x)
444d8589bd5SBoris Popov {
445d8589bd5SBoris Popov int64_t v;
446d8589bd5SBoris Popov int error;
447d8589bd5SBoris Popov
448d8589bd5SBoris Popov error = md_get_int64(mdp, &v);
44912f65109SJohn Baldwin if (x != NULL)
4500adb6d7aSRobert Drehmel *x = be64toh(v);
4510ca2381dSKevin Lo return (error);
452d8589bd5SBoris Popov }
453d8589bd5SBoris Popov
454d8589bd5SBoris Popov int
md_get_int64le(struct mdchain * mdp,int64_t * x)455d8589bd5SBoris Popov md_get_int64le(struct mdchain *mdp, int64_t *x)
456d8589bd5SBoris Popov {
457d8589bd5SBoris Popov int64_t v;
458d8589bd5SBoris Popov int error;
459d8589bd5SBoris Popov
460d8589bd5SBoris Popov error = md_get_int64(mdp, &v);
46112f65109SJohn Baldwin if (x != NULL)
4620adb6d7aSRobert Drehmel *x = le64toh(v);
4630ca2381dSKevin Lo return (error);
464d8589bd5SBoris Popov }
465d8589bd5SBoris Popov
466d8589bd5SBoris Popov int
md_get_mem(struct mdchain * mdp,caddr_t target,int size,int type)467d8589bd5SBoris Popov md_get_mem(struct mdchain *mdp, caddr_t target, int size, int type)
468d8589bd5SBoris Popov {
469d8589bd5SBoris Popov struct mbuf *m = mdp->md_cur;
470d8589bd5SBoris Popov int error;
471d8589bd5SBoris Popov u_int count;
472d8589bd5SBoris Popov u_char *s;
473d8589bd5SBoris Popov
474d8589bd5SBoris Popov while (size > 0) {
475d8589bd5SBoris Popov if (m == NULL) {
476d8589bd5SBoris Popov MBERROR("incomplete copy\n");
4770ca2381dSKevin Lo return (EBADRPC);
478d8589bd5SBoris Popov }
479d8589bd5SBoris Popov s = mdp->md_pos;
480d8589bd5SBoris Popov count = mtod(m, u_char*) + m->m_len - s;
481d8589bd5SBoris Popov if (count == 0) {
482d8589bd5SBoris Popov mdp->md_cur = m = m->m_next;
483d8589bd5SBoris Popov if (m)
484d8589bd5SBoris Popov s = mdp->md_pos = mtod(m, caddr_t);
485d8589bd5SBoris Popov continue;
486d8589bd5SBoris Popov }
487d8589bd5SBoris Popov if (count > size)
488d8589bd5SBoris Popov count = size;
489d8589bd5SBoris Popov size -= count;
490d8589bd5SBoris Popov mdp->md_pos += count;
491d8589bd5SBoris Popov if (target == NULL)
492d8589bd5SBoris Popov continue;
493d8589bd5SBoris Popov switch (type) {
494d8589bd5SBoris Popov case MB_MUSER:
495d8589bd5SBoris Popov error = copyout(s, target, count);
496d8589bd5SBoris Popov if (error)
497d8589bd5SBoris Popov return error;
498d8589bd5SBoris Popov break;
499d8589bd5SBoris Popov case MB_MSYSTEM:
500d8589bd5SBoris Popov bcopy(s, target, count);
501d8589bd5SBoris Popov break;
502d8589bd5SBoris Popov case MB_MINLINE:
503d8589bd5SBoris Popov while (count--)
504d8589bd5SBoris Popov *target++ = *s++;
505d8589bd5SBoris Popov continue;
506d8589bd5SBoris Popov }
507d8589bd5SBoris Popov target += count;
508d8589bd5SBoris Popov }
5090ca2381dSKevin Lo return (0);
510d8589bd5SBoris Popov }
511d8589bd5SBoris Popov
512d8589bd5SBoris Popov int
md_get_mbuf(struct mdchain * mdp,int size,struct mbuf ** ret)513d8589bd5SBoris Popov md_get_mbuf(struct mdchain *mdp, int size, struct mbuf **ret)
514d8589bd5SBoris Popov {
515d8589bd5SBoris Popov struct mbuf *m = mdp->md_cur, *rm;
516d8589bd5SBoris Popov
517eb1b1807SGleb Smirnoff rm = m_copym(m, mdp->md_pos - mtod(m, u_char*), size, M_WAITOK);
518d8589bd5SBoris Popov md_get_mem(mdp, NULL, size, MB_MZERO);
519d8589bd5SBoris Popov *ret = rm;
5200ca2381dSKevin Lo return (0);
521d8589bd5SBoris Popov }
522d8589bd5SBoris Popov
523d8589bd5SBoris Popov int
md_get_uio(struct mdchain * mdp,struct uio * uiop,int size)524d8589bd5SBoris Popov md_get_uio(struct mdchain *mdp, struct uio *uiop, int size)
525d8589bd5SBoris Popov {
526d8589bd5SBoris Popov char *uiocp;
527d8589bd5SBoris Popov long left;
528d8589bd5SBoris Popov int mtype, error;
529d8589bd5SBoris Popov
530d8589bd5SBoris Popov mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER;
531cebcee2eSBoris Popov while (size > 0 && uiop->uio_resid) {
532d8589bd5SBoris Popov if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
5330ca2381dSKevin Lo return (EFBIG);
534d8589bd5SBoris Popov left = uiop->uio_iov->iov_len;
535cebcee2eSBoris Popov if (left == 0) {
536cebcee2eSBoris Popov uiop->uio_iov++;
537cebcee2eSBoris Popov uiop->uio_iovcnt--;
538cebcee2eSBoris Popov continue;
539cebcee2eSBoris Popov }
540d8589bd5SBoris Popov uiocp = uiop->uio_iov->iov_base;
541d8589bd5SBoris Popov if (left > size)
542d8589bd5SBoris Popov left = size;
543d8589bd5SBoris Popov error = md_get_mem(mdp, uiocp, left, mtype);
544d8589bd5SBoris Popov if (error)
5450ca2381dSKevin Lo return (error);
546d8589bd5SBoris Popov uiop->uio_offset += left;
547d8589bd5SBoris Popov uiop->uio_resid -= left;
5482b7f24d2SMike Barcroft uiop->uio_iov->iov_base =
5492b7f24d2SMike Barcroft (char *)uiop->uio_iov->iov_base + left;
550d8589bd5SBoris Popov uiop->uio_iov->iov_len -= left;
551d8589bd5SBoris Popov size -= left;
552d8589bd5SBoris Popov }
5530ca2381dSKevin Lo return (0);
554d8589bd5SBoris Popov }
555