xref: /freebsd/share/man/man9/uio.9 (revision f0a75d274af375d15b97b830966b99a02b7db911)
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$
27.\"
28.Dd December 8, 2006
29.Os
30.Dt UIO 9
31.Sh NAME
32.Nm uio ,
33.Nm uiomove
34.Nd device driver I/O routines
35.Sh SYNOPSIS
36.In sys/types.h
37.In sys/uio.h
38.Pp
39.Bd -literal
40struct uio {
41	struct	iovec *uio_iov;		/* scatter/gather list */
42	int	uio_iovcnt;		/* length of scatter/gather list */
43	off_t	uio_offset;		/* offset in target object */
44	int	uio_resid;		/* remaining bytes to copy */
45	enum	uio_seg uio_segflg;	/* address space */
46	enum	uio_rw uio_rw;		/* operation */
47	struct	thread *uio_td;		/* owner */
48};
49.Ed
50.Ft int
51.Fn uiomove "void *buf" "int howmuch" "struct uio *uiop"
52.Sh DESCRIPTION
53The function
54.Fn uiomove
55is used to handle transfer of data between buffers and I/O vectors
56that might possibly also cross the user/kernel space boundary.
57.Pp
58As a result of any
59.Xr read 2 ,
60.Xr write 2 ,
61.Xr readv 2 ,
62or
63.Xr writev 2
64system call that is being passed to a character-device driver, the
65appropriate driver
66.Va d_read
67or
68.Va d_write
69entry will be called with a pointer to a
70.Vt "struct uio"
71being passed.
72The transfer request is encoded in this structure.
73The driver itself should use
74.Fn uiomove
75to get at the data in this structure.
76.Pp
77The fields in the
78.Vt uio
79structure are:
80.Bl -tag -width ".Va uio_iovcnt"
81.It Va uio_iov
82The array of I/O vectors to be processed.
83In the case of scatter/gather
84I/O, this will be more than one vector.
85.It Va uio_iovcnt
86The number of I/O vectors present.
87.It Va uio_offset
88The offset into the device.
89.It Va uio_resid
90The remaining number of bytes to process, updated after transfer.
91.It Va uio_segflg
92One of the following flags:
93.Bl -tag -width ".Dv UIO_USERSPACE"
94.It Dv UIO_USERSPACE
95The I/O vector points into a process's address space.
96.It Dv UIO_SYSSPACE
97The I/O vector points into the kernel address space.
98.It Dv UIO_NOCOPY
99Do not copy, already in object.
100.El
101.It Va uio_rw
102The direction of the desired transfer, either
103.Dv UIO_READ ,
104or
105.Dv UIO_WRITE .
106.It Va uio_td
107The pointer to a
108.Vt "struct thread"
109for the associated thread; used if
110.Va uio_segflg
111indicates that the transfer is to be made from/to a process's address
112space.
113.El
114.Sh RETURN VALUES
115On success
116.Fn uiomove
117will return 0, on error it will return an appropriate errno.
118.Sh ERRORS
119.Fn uiomove
120will fail and return the following error code if:
121.Bl -tag -width Er
122.It Bq Er EFAULT
123The invoked
124.Xr copyin 9
125or
126.Xr copyout 9
127returned
128.Er EFAULT
129.Sh EXAMPLES
130The idea is that the driver maintains a private buffer for its data,
131and processes the request in chunks of maximal the size of this
132buffer.
133Note that the buffer handling below is very simplified and
134will not work (the buffer pointer is not being advanced in case of a
135partial read), it is just here to demonstrate the
136.Nm
137handling.
138.Bd -literal
139/* MIN() can be found there: */
140#include <sys/param.h>
141
142#define BUFSIZE 512
143static char buffer[BUFSIZE];
144
145static int data_available;	/* amount of data that can be read */
146
147static int
148fooread(dev_t dev, struct uio *uio, int flag)
149{
150	int rv, amnt;
151
152	rv = 0;
153	while (uio->uio_resid > 0) {
154		if (data_available > 0) {
155			amnt = MIN(uio->uio_resid, data_available);
156			rv = uiomove(buffer, amnt, uio);
157			if (rv != 0)
158				break;
159			data_available -= amnt;
160		} else
161			tsleep(...);	/* wait for a better time */
162	}
163	if (rv != 0) {
164		/* do error cleanup here */
165	}
166	return (rv);
167}
168.Ed
169.Sh SEE ALSO
170.Xr read 2 ,
171.Xr readv 2 ,
172.Xr write 2 ,
173.Xr writev 2 ,
174.Xr copyin 9 ,
175.Xr copyout 9 ,
176.Xr sleep 9
177.Sh HISTORY
178The
179.Nm
180mechanism appeared in some early version of
181.Ux .
182.Sh AUTHORS
183This manual page was written by
184.An J\(:org Wunsch .
185