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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /*        All Rights Reserved   */
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 /*
40  * $FreeBSD$
41  */
42 
43 #include <sys/types.h>
44 #include <sys/uio.h>
45 #include <sys/vnode.h>
46 
47 /*
48  * same as uiomove() but doesn't modify uio structure.
49  * return in cbytes how many bytes were copied.
50  */
51 int
52 uiocopy(void *p, size_t n, enum uio_rw rw, struct uio *uio, size_t *cbytes)
53 {
54 	struct iovec small_iovec[1];
55 	struct uio small_uio_clone;
56 	struct uio *uio_clone;
57 	int error;
58 
59 	ASSERT3U(uio->uio_rw, ==, rw);
60 	if (uio->uio_iovcnt == 1) {
61 		small_uio_clone = *uio;
62 		small_iovec[0] = *uio->uio_iov;
63 		small_uio_clone.uio_iov = small_iovec;
64 		uio_clone = &small_uio_clone;
65 	} else {
66 		uio_clone = cloneuio(uio);
67 	}
68 
69 	error = vn_io_fault_uiomove(p, n, uio_clone);
70 	*cbytes = uio->uio_resid - uio_clone->uio_resid;
71 	if (uio_clone != &small_uio_clone)
72 		free(uio_clone, M_IOV);
73 	return (error);
74 }
75 
76 /*
77  * Drop the next n chars out of *uiop.
78  */
79 void
80 uioskip(uio_t *uio, size_t n)
81 {
82 	enum uio_seg segflg;
83 
84 	/* For the full compatibility with illumos. */
85 	if (n > uio->uio_resid)
86 		return;
87 
88 	segflg = uio->uio_segflg;
89 	uio->uio_segflg = UIO_NOCOPY;
90 	uiomove(NULL, n, uio->uio_rw, uio);
91 	uio->uio_segflg = segflg;
92 }
93