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