Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: uio.h,v 1.13 2025/07/24 09:04:56 hans Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd (at) FreeBSD.org>
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  *
     45  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
     46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
     49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  * SUCH DAMAGE.
     56  *
     57  * $FreeBSD: head/sys/cddl/compat/opensolaris/sys/uio.h 219089 2011-02-27 19:41:40Z pjd $
     58  */
     59 
     60 #ifndef _OPENSOLARIS_SYS_UIO_H_
     61 #define	_OPENSOLARIS_SYS_UIO_H_
     62 
     63 #include_next <sys/uio.h>
     64 
     65 #ifndef __sun
     66 #include <sys/debug.h>
     67 
     68 #ifndef _KERNEL
     69 #include <assert.h>
     70 #include <string.h>
     71 
     72 #define	FOF_OFFSET	1	/* Use the offset in uio argument */
     73 
     74 struct uio {
     75 	struct	iovec *uio_iov;
     76 	int	uio_iovcnt;
     77 	off_t	uio_offset;
     78 	int	uio_resid;
     79 	enum	uio_seg uio_segflg;
     80 	enum	uio_rw uio_rw;
     81 	void	*uio_td;
     82 };
     83 #endif
     84 
     85 struct xuio {
     86 	struct uio xu_uio;
     87 	int	xuio_rw;
     88 	void *xuio_priv;
     89 };
     90 
     91 #define XUIO_XUZC_PRIV(xuio)	((xuio)->xuio_priv)
     92 #define XUIO_XUZC_RW(xuio)	((xuio)->xuio_rw)
     93 
     94 typedef	struct uio	uio_t;
     95 typedef struct xuio	xuio_t;
     96 typedef	struct iovec	iovec_t;
     97 
     98 typedef enum uio_seg    uio_seg_t;
     99 
    100 #define	uio_loffset	uio_offset
    101 
    102 int	uiomove(void *, size_t, struct uio *);
    103 
    104 static __inline int
    105 zfs_uiomove(void *cp, size_t n, enum uio_rw dir, uio_t *uio)
    106 {
    107 
    108 	assert(uio->uio_rw == dir);
    109 	return (uiomove(cp, n, uio));
    110 }
    111 
    112 #define	ZFS_MIN(a,b)	((/*CONSTCOND*/(a)<(b))?(a):(b))
    113 
    114 static __inline int
    115 zfs_uiocopy(void *cp, size_t n, enum uio_rw dir, uio_t *uio, size_t *cbytes)
    116 {
    117 	uio_t auio;
    118 	struct iovec aiov;
    119 	size_t cnt;
    120 	int i, error;
    121 
    122 	*cbytes = 0;
    123 	memcpy(&auio, uio, sizeof(*uio));
    124 	for (i = 0; i < uio->uio_iovcnt && n > 0; i++) {
    125 		auio.uio_iov = &aiov;
    126 		auio.uio_iovcnt = 1;
    127 		aiov = uio->uio_iov[i];
    128 		cnt = ZFS_MIN(aiov.iov_len, n);
    129 		if (cnt == 0)
    130 			continue;
    131 		error = uiomove(cp, cnt, &auio);
    132 		if (error)
    133 			return error;
    134 		cp = (char *)cp + cnt;
    135 		n -= cnt;
    136 		*cbytes += cnt;
    137 	}
    138 
    139 	return 0;
    140 }
    141 
    142 static __inline void
    143 zfs_uioskip(uio_t *uiop, size_t n)
    144 {
    145 	if (n > (size_t)uiop->uio_resid)
    146 		return;
    147 	while (n != 0) {
    148 		iovec_t        *iovp = uiop->uio_iov;
    149 		size_t         niovb = ZFS_MIN(iovp->iov_len, n);
    150 
    151 		if (niovb == 0) {
    152 			uiop->uio_iov++;
    153 			uiop->uio_iovcnt--;
    154 			continue;
    155 		}
    156 		iovp->iov_base = (char *)iovp->iov_base + niovb;
    157 		uiop->uio_offset += (off_t)niovb;
    158 		iovp->iov_len -= niovb;
    159 		uiop->uio_resid -= (int)niovb;
    160 		n -= niovb;
    161 	}
    162 
    163 }
    164 
    165 #define	uiomove(cp, n, dir, uio)	zfs_uiomove((cp), (n), (dir), (uio))
    166 #define uiocopy(cp, n, dir, uio, cbytes) 	zfs_uiocopy((cp), (n), (dir), (uio), (cbytes))
    167 #define uioskip(uio, size) 		zfs_uioskip((uio), (size))
    168 
    169 #endif	/* __sun */
    170 
    171 #endif	/* !_OPENSOLARIS_SYS_UIO_H_ */
    172