1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30 /*
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
34 *
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
38 */
39
40 #ifndef _LIBSPL_SYS_UIO_H
41 #define _LIBSPL_SYS_UIO_H
42
43 #include <sys/types.h>
44 #include_next <sys/uio.h>
45
46 #ifdef __APPLE__
47 #include <sys/_types/_iovec_t.h>
48 #endif
49
50 #include <stdint.h>
51 typedef struct iovec iovec_t;
52
53 #if defined(__linux__) || defined(__APPLE__)
54 typedef enum zfs_uio_rw {
55 UIO_READ = 0,
56 UIO_WRITE = 1,
57 } zfs_uio_rw_t;
58
59 typedef enum zfs_uio_seg {
60 UIO_USERSPACE = 0,
61 UIO_SYSSPACE = 1,
62 } zfs_uio_seg_t;
63
64 #elif defined(__FreeBSD__)
65 typedef enum uio_seg zfs_uio_seg_t;
66 #endif
67
68 typedef struct zfs_uio {
69 struct iovec *uio_iov; /* pointer to array of iovecs */
70 int uio_iovcnt; /* number of iovecs */
71 offset_t uio_loffset; /* file offset */
72 zfs_uio_seg_t uio_segflg; /* address space (kernel or user) */
73 uint16_t uio_fmode; /* file mode flags */
74 uint16_t uio_extflg; /* extended flags */
75 ssize_t uio_resid; /* residual count */
76 } zfs_uio_t;
77
78 #define zfs_uio_segflg(uio) (uio)->uio_segflg
79 #define zfs_uio_offset(uio) (uio)->uio_loffset
80 #define zfs_uio_resid(uio) (uio)->uio_resid
81 #define zfs_uio_iovcnt(uio) (uio)->uio_iovcnt
82 #define zfs_uio_iovlen(uio, idx) (uio)->uio_iov[(idx)].iov_len
83 #define zfs_uio_iovbase(uio, idx) (uio)->uio_iov[(idx)].iov_base
84
85 static inline void
zfs_uio_iov_at_index(zfs_uio_t * uio,uint_t idx,void ** base,uint64_t * len)86 zfs_uio_iov_at_index(zfs_uio_t *uio, uint_t idx, void **base, uint64_t *len)
87 {
88 *base = zfs_uio_iovbase(uio, idx);
89 *len = zfs_uio_iovlen(uio, idx);
90 }
91
92 static inline void
zfs_uio_advance(zfs_uio_t * uio,ssize_t size)93 zfs_uio_advance(zfs_uio_t *uio, ssize_t size)
94 {
95 uio->uio_resid -= size;
96 uio->uio_loffset += size;
97 }
98
99 static inline offset_t
zfs_uio_index_at_offset(zfs_uio_t * uio,offset_t off,uint_t * vec_idx)100 zfs_uio_index_at_offset(zfs_uio_t *uio, offset_t off, uint_t *vec_idx)
101 {
102 *vec_idx = 0;
103 while (*vec_idx < (uint_t)zfs_uio_iovcnt(uio) &&
104 off >= (offset_t)zfs_uio_iovlen(uio, *vec_idx)) {
105 off -= zfs_uio_iovlen(uio, *vec_idx);
106 (*vec_idx)++;
107 }
108
109 return (off);
110 }
111
112 #endif /* _SYS_UIO_H */
113