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