xref: /dragonfly/share/man/man9/uio.9 (revision 38a690d7)
1.\"
2.\" Copyright (c) 1997 Joerg Wunsch
3.\"
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25.\"
26.\" $FreeBSD: src/share/man/man9/uio.9,v 1.5.2.4 2001/12/17 11:30:19 ru Exp $
27.\" $DragonFly: src/share/man/man9/uio.9,v 1.2 2003/06/17 04:37:01 dillon Exp $
28.\"
29.Dd February 2, 1997
30.Os
31.Dt UIO 9
32.Sh NAME
33.Nm uio ,
34.Nm uiomove
35.Nd device driver I/O routines
36.Sh SYNOPSIS
37.In sys/types.h
38.In sys/uio.h
39.Pp
40.Bd -literal
41struct uio {
42	struct	iovec *uio_iov;
43	int	uio_iovcnt;
44	off_t	uio_offset;
45	int	uio_resid;
46	enum	uio_seg uio_segflg;
47	enum	uio_rw uio_rw;
48	struct	proc *uio_procp;
49};
50.Ed
51.Ft int
52.Fn uiomove "caddr_t buf" "int howmuch" "struct uio *uiop"
53.Sh DESCRIPTION
54The function
55.Fn uiomove
56is used to handle transfer of data between buffers and I/O vectors
57that might possibly also cross the user/kernel space boundary.
58.Pp
59As a result of any
60.Xr read 2 ,
61.Xr write 2 ,
62.Xr readv 2 ,
63or
64.Xr writev 2
65system call that is being passed to a character-device driver, the
66appropriate driver
67.Va d_read
68or
69.Va d_write
70entry will be called with a pointer to a
71.Vt "struct uio"
72being passed.
73The transfer request is encoded in this structure.
74The driver itself should use
75.Fn uiomove
76to get at the data in this structure.
77.Pp
78The fields in the
79.Vt uio
80structure are:
81.Bl -tag -width ".Va uio_iovcnt"
82.It Va uio_iov
83The array of I/O vectors to be processed.
84In the case of scatter/gather
85I/O, this will be more than one vector.
86.It Va uio_iovcnt
87The number of I/O vectors present.
88.It Va uio_offset
89The offset into the device.
90.It Va uio_resid
91The number of bytes to process.
92.It Va uio_segflg
93One of the following flags:
94.Bl -tag -width ".Dv UIO_USERISPACE"
95.It Dv UIO_USERSPACE
96The I/O vector points into a process's address space.
97.It Dv UIO_SYSSPACE
98The I/O vector points into the kernel address space.
99.It Dv UIO_USERISPACE
100The I/O vector points into the instruction area of a process's address
101space.
102.It Dv UIO_NOCOPY
103Don't copy, already in object.
104.El
105.It Va uio_rw
106The direction of the desired transfer, either
107.Dv UIO_READ ,
108or
109.Dv UIO_WRITE .
110.It Va uio_td
111The pointer to a
112.Vt "struct thread"
113for the associated thread; used if
114.Va uio_segflg
115indicates that the transfer is to be made from/to a process's address
116space.
117.El
118.Sh EXAMPLES
119The idea is that the driver maintains a private buffer for its data,
120and processes the request in chunks of maximal the size of this
121buffer.
122Note that the buffer handling below is very simplified and
123won't work (the buffer pointer is not being advanced in case of a
124partial read), it's just here to demonstrate the
125.Nm
126handling.
127.Bd -literal
128/* MIN() can be found there: */
129#include <sys/param.h>
130
131#define BUFSIZE 512
132static char buffer[BUFSIZE];
133
134static int data_available;	/* amount of data that can be read */
135
136static int
137fooread(dev_t dev, struct uio *uio, int flag)
138{
139	int rv, amnt;
140
141	while (uio->uio_resid > 0) {
142		if (data_available > 0) {
143			amnt = MIN(uio->uio_resid, data_available);
144			if ((rv = uiomove((caddr_t)buffer, amnt, uio))
145			    != 0)
146				goto error;
147			data_available -= amnt;
148		} else {
149			tsleep(...);	/* wait for a better time */
150		}
151	}
152	return 0;
153error:
154	/* do error cleanup here */
155	return rv;
156}
157.Ed
158.Sh RETURN VALUES
159.Fn uiomove
160can return
161.Er EFAULT
162from the invoked
163.Xr copyin 9
164or
165.Xr copyout 9
166in case the transfer was to/from a process's address space.
167.Sh SEE ALSO
168.Xr read 2 ,
169.Xr readv 2 ,
170.Xr write 2 ,
171.Xr writev 2 ,
172.Xr copyin 9 ,
173.Xr copyout 9 ,
174.Xr sleep 9
175.Sh HISTORY
176The
177.Nm
178mechanism appeared in some early version of
179.Ux .
180.Sh AUTHORS
181This man page was written by
182.An J\(:org Wunsch .
183