1 /* $OpenBSD: kern_physio.c,v 1.49 2024/02/03 18:51:58 beck Exp $ */
2 /* $NetBSD: kern_physio.c,v 1.28 1997/05/19 10:43:28 pk Exp $ */
3
4 /*-
5 * Copyright (c) 1994 Christopher G. Demetriou
6 * Copyright (c) 1982, 1986, 1990, 1993
7 * The Regents of the University of California. All rights reserved.
8 * (c) UNIX System Laboratories, Inc.
9 * All or some portions of this file are derived from material licensed
10 * to the University of California by American Telephone and Telegraph
11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12 * the permission of UNIX System Laboratories, Inc.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/buf.h>
44 #include <sys/pool.h>
45
46 #include <uvm/uvm_extern.h>
47
48 /*
49 * The routines implemented in this file are described in:
50 * Leffler, et al.: The Design and Implementation of the 4.3BSD
51 * UNIX Operating System (Addison Welley, 1989)
52 * on pages 231-233.
53 */
54
55 /*
56 * Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly
57 * from the raw device to user buffers, and bypasses the buffer cache.
58 *
59 * Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
60 */
61 int
physio(void (* strategy)(struct buf *),dev_t dev,int flags,void (* minphys)(struct buf *),struct uio * uio)62 physio(void (*strategy)(struct buf *), dev_t dev, int flags,
63 void (*minphys)(struct buf *), struct uio *uio)
64 {
65 struct iovec *iovp;
66 struct proc *p = curproc;
67 long done, todo;
68 int error, i, s;
69 struct buf *bp;
70
71 if ((uio->uio_offset % DEV_BSIZE) != 0)
72 return (EINVAL);
73
74 error = 0;
75 flags &= B_READ | B_WRITE;
76
77 /* Create a buffer. */
78 s = splbio();
79 bp = pool_get(&bufpool, PR_WAITOK | PR_ZERO);
80
81 /* [set up the fixed part of the buffer for a transfer] */
82 bp->b_vnbufs.le_next = NOLIST;
83 bp->b_dev = dev;
84 bp->b_error = 0;
85 bp->b_proc = p;
86 bp->b_flags = B_BUSY;
87 splx(s);
88
89 /*
90 * [while there are data to transfer and no I/O error]
91 * Note that I/O errors are handled with a 'goto' at the bottom
92 * of the 'while' loop.
93 */
94 for (i = 0; i < uio->uio_iovcnt; i++) {
95 iovp = &uio->uio_iov[i];
96 while (iovp->iov_len > 0) {
97 void *map = NULL;
98
99 /*
100 * [mark the buffer busy for physical I/O]
101 * (i.e. set B_PHYS (because it's an I/O to user
102 * memory), and B_RAW, because B_RAW is to be
103 * "Set by physio for raw transfers.", in addition
104 * to the "busy" and read/write flag.)
105 */
106 CLR(bp->b_flags, B_DONE | B_ERROR);
107 bp->b_flags |= (B_BUSY | B_PHYS | B_RAW | flags);
108
109 /* [set up the buffer for a maximum-sized transfer] */
110 bp->b_blkno = btodb(uio->uio_offset);
111
112 /*
113 * Because iov_len is size_t (unsigned) but b_bcount is
114 * long (signed), an overflow is possible. Therefore
115 * limit b_bcount to LONG_MAX before calling the
116 * provided minphys.
117 */
118 if (iovp->iov_len > LONG_MAX)
119 bp->b_bcount = LONG_MAX;
120 else
121 bp->b_bcount = iovp->iov_len;
122
123 /*
124 * [call minphys to bound the transfer size]
125 * and remember the amount of data to transfer,
126 * for later comparison.
127 */
128 (*minphys)(bp);
129 todo = bp->b_bcount;
130 KASSERTMSG(todo >= 0, "minphys broken");
131
132 /*
133 * [lock the part of the user address space involved
134 * in the transfer]
135 * Beware vmapbuf(); it clobbers b_data and
136 * saves it in b_saveaddr. However, vunmapbuf()
137 * restores it.
138 */
139 error = uvm_vslock_device(p, iovp->iov_base, todo,
140 (flags & B_READ) ?
141 PROT_READ | PROT_WRITE : PROT_READ, &map);
142 if (error)
143 goto done;
144 if (map) {
145 bp->b_data = map;
146 } else {
147 bp->b_data = iovp->iov_base;
148 vmapbuf(bp, todo);
149 }
150
151 /* [call strategy to start the transfer] */
152 (*strategy)(bp);
153
154 /*
155 * Note that the raise/wait/lower/get error
156 * steps below would be done by biowait(), but
157 * we want to unlock the address space before
158 * we lower the priority.
159 *
160 * [raise the priority level to splbio]
161 */
162 s = splbio();
163
164 /* [wait for the transfer to complete] */
165 while ((bp->b_flags & B_DONE) == 0)
166 tsleep_nsec(bp, PRIBIO + 1, "physio", INFSLP);
167
168 /* Mark it busy again, so nobody else will use it. */
169 bp->b_flags |= B_BUSY;
170
171 /* [lower the priority level] */
172 splx(s);
173
174 /*
175 * [unlock the part of the address space previously
176 * locked]
177 */
178 if (!map)
179 vunmapbuf(bp, todo);
180 uvm_vsunlock_device(p, iovp->iov_base, todo, map);
181
182 /* remember error value (save a splbio/splx pair) */
183 if (bp->b_flags & B_ERROR)
184 error = (bp->b_error ? bp->b_error : EIO);
185
186 /*
187 * [deduct the transfer size from the total number
188 * of data to transfer]
189 */
190 KASSERTMSG(bp->b_resid <= LONG_MAX, "strategy broken");
191 done = bp->b_bcount - bp->b_resid;
192 KASSERTMSG(done >= 0, "strategy broken");
193 KASSERTMSG(done <= todo, "strategy broken");
194 iovp->iov_len -= done;
195 iovp->iov_base = (caddr_t)iovp->iov_base + done;
196 uio->uio_offset += done;
197 uio->uio_resid -= done;
198
199 /*
200 * Now, check for an error.
201 * Also, handle weird end-of-disk semantics.
202 */
203 if (error || done < todo)
204 goto done;
205 }
206 }
207
208 done:
209 /*
210 * [clean up the state of the buffer]
211 */
212 s = splbio();
213 /* XXXCDC: is this necessary? */
214 if (bp->b_vp)
215 brelvp(bp);
216 splx(s);
217 pool_put(&bufpool, bp);
218
219 return (error);
220 }
221
222 /*
223 * Leffler, et al., says on p. 231:
224 * "The minphys() routine is called by physio() to adjust the
225 * size of each I/O transfer before the latter is passed to
226 * the strategy routine..."
227 *
228 * so, just adjust the buffer's count accounting to MAXPHYS here,
229 * and return the new count;
230 */
231 void
minphys(struct buf * bp)232 minphys(struct buf *bp)
233 {
234
235 if (bp->b_bcount > MAXPHYS)
236 bp->b_bcount = MAXPHYS;
237 }
238