root/usr/src/lib/libfakekernel/common/uio.c
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
 */

#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/uio.h>
#include <sys/errno.h>

/*
 * Move "n" bytes at byte address "p"; "rw" indicates the direction
 * of the move, and the I/O parameters are provided in "uio", which is
 * update to reflect the data which was moved.  Returns 0 on success or
 * a non-zero errno on failure.
 */
int
uiomove(void *p, size_t n, enum uio_rw rw, struct uio *uio)
{
        struct iovec *iov;
        ulong_t cnt;

        while (n && uio->uio_resid) {
                iov = uio->uio_iov;
                cnt = MIN(iov->iov_len, n);
                if (cnt == 0L) {
                        uio->uio_iov++;
                        uio->uio_iovcnt--;
                        continue;
                }
                switch (uio->uio_segflg) {

                case UIO_USERISPACE:
                        return (EINVAL);

                case UIO_USERSPACE:
                case UIO_SYSSPACE:
                        if (rw == UIO_READ)
                                bcopy(p, iov->iov_base, cnt);
                        else
                                bcopy(iov->iov_base, p, cnt);
                        break;
                }
                iov->iov_base += cnt;
                iov->iov_len -= cnt;
                uio->uio_resid -= cnt;
                uio->uio_loffset += cnt;
                p = (caddr_t)p + cnt;
                n -= cnt;
        }
        return (0);
}

/*
 * Drop the next n chars out of *uiop.
 */
void
uioskip(uio_t *uiop, size_t n)
{
        if (n > uiop->uio_resid)
                return;
        while (n != 0) {
                iovec_t *iovp = uiop->uio_iov;
                size_t  niovb = MIN(iovp->iov_len, n);

                if (niovb == 0) {
                        uiop->uio_iov++;
                        uiop->uio_iovcnt--;
                        continue;
                }
                iovp->iov_base += niovb;
                uiop->uio_loffset += niovb;
                iovp->iov_len -= niovb;
                uiop->uio_resid -= niovb;
                n -= niovb;
        }
}