2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1994 John S. Dyson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Absolutely no warranty of function or purpose is made by the author
18 * 4. Modifications may be freely made to this file if the above conditions
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
25 #include <sys/param.h>
26 #include <sys/systm.h>
30 #include <sys/malloc.h>
32 #include <sys/racct.h>
34 #include <geom/geom.h>
37 #include <vm/vm_page.h>
38 #include <vm/vm_extern.h>
39 #include <vm/vm_map.h>
42 physio(struct cdev *dev, struct uio *uio, int ioflag)
47 struct vm_page **pages;
50 int error, i, npages, maxpages;
56 /* check if character device is being destroyed */
60 /* XXX: sanity check */
61 if(dev->si_iosize_max < PAGE_SIZE) {
62 printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
63 devtoname(dev), dev->si_iosize_max);
64 dev->si_iosize_max = DFLTPHYS;
68 * If the driver does not want I/O to be split, that means that we
69 * need to reject any requests that will not fit into one buffer.
71 if (dev->si_flags & SI_NOSPLIT &&
72 (uio->uio_resid > dev->si_iosize_max || uio->uio_resid > maxphys ||
73 uio->uio_iovcnt > 1)) {
75 * Tell the user why his I/O was rejected.
77 if (uio->uio_resid > dev->si_iosize_max)
78 uprintf("%s: request size=%zd > si_iosize_max=%d; "
79 "cannot split request\n", devtoname(dev),
80 uio->uio_resid, dev->si_iosize_max);
81 if (uio->uio_resid > maxphys)
82 uprintf("%s: request size=%zd > maxphys=%lu; "
83 "cannot split request\n", devtoname(dev),
84 uio->uio_resid, maxphys);
85 if (uio->uio_iovcnt > 1)
86 uprintf("%s: request vectors=%d > 1; "
87 "cannot split request\n", devtoname(dev),
93 * Keep the process UPAGES from being swapped. Processes swapped
94 * out while holding pbufs, used by swapper, may lead to deadlock.
99 if (uio->uio_segflg != UIO_USERSPACE) {
102 } else if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) {
104 maxpages = btoc(MIN(uio->uio_resid, maxphys)) + 1;
105 pages = malloc(sizeof(*pages) * maxpages, M_DEVBUF, M_WAITOK);
107 pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
108 MPASS((pbuf->b_flags & B_MAXPHYS) != 0);
110 maxpages = btoc(maxphys);
111 pages = pbuf->b_pages;
114 if (uio->uio_rw == UIO_READ)
115 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
117 for (i = 0; i < uio->uio_iovcnt; i++) {
121 if (uio->uio_rw == UIO_READ) {
122 racct_add_force(curproc, RACCT_READBPS,
123 uio->uio_iov[i].iov_len);
124 racct_add_force(curproc, RACCT_READIOPS, 1);
126 racct_add_force(curproc, RACCT_WRITEBPS,
127 uio->uio_iov[i].iov_len);
128 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
130 PROC_UNLOCK(curproc);
134 while (uio->uio_iov[i].iov_len) {
136 if (uio->uio_rw == UIO_READ) {
137 bp->bio_cmd = BIO_READ;
138 curthread->td_ru.ru_inblock++;
140 bp->bio_cmd = BIO_WRITE;
141 curthread->td_ru.ru_oublock++;
143 bp->bio_offset = uio->uio_offset;
144 base = uio->uio_iov[i].iov_base;
145 bp->bio_length = uio->uio_iov[i].iov_len;
146 if (bp->bio_length > dev->si_iosize_max)
147 bp->bio_length = dev->si_iosize_max;
148 if (bp->bio_length > maxphys)
149 bp->bio_length = maxphys;
152 * Make sure the pbuf can map the request.
153 * The pbuf has kvasize = maxphys, so a request
154 * larger than maxphys - PAGE_SIZE must be
155 * page aligned or it will be fragmented.
157 poff = (vm_offset_t)base & PAGE_MASK;
158 if (pbuf && bp->bio_length + poff > pbuf->b_kvasize) {
159 if (dev->si_flags & SI_NOSPLIT) {
160 uprintf("%s: request ptr %p is not "
161 "on a page boundary; cannot split "
162 "request\n", devtoname(dev),
167 bp->bio_length = pbuf->b_kvasize;
169 bp->bio_length -= PAGE_SIZE;
172 bp->bio_bcount = bp->bio_length;
176 if ((npages = vm_fault_quick_hold_pages(
177 &curproc->p_vmspace->vm_map,
178 (vm_offset_t)base, bp->bio_length,
179 prot, pages, maxpages)) < 0) {
184 pmap_qenter((vm_offset_t)sa,
186 bp->bio_data = sa + poff;
189 bp->bio_ma_n = npages;
190 bp->bio_ma_offset = poff;
191 bp->bio_data = unmapped_buf;
192 bp->bio_flags |= BIO_UNMAPPED;
198 if (uio->uio_rw == UIO_READ)
199 biowait(bp, "physrd");
201 biowait(bp, "physwr");
205 pmap_qremove((vm_offset_t)sa, npages);
206 vm_page_unhold_pages(pages, npages);
209 iolen = bp->bio_length - bp->bio_resid;
210 if (iolen == 0 && !(bp->bio_flags & BIO_ERROR))
211 goto doerror; /* EOF */
212 uio->uio_iov[i].iov_len -= iolen;
213 uio->uio_iov[i].iov_base =
214 (char *)uio->uio_iov[i].iov_base + iolen;
215 uio->uio_resid -= iolen;
216 uio->uio_offset += iolen;
217 if (bp->bio_flags & BIO_ERROR) {
218 error = bp->bio_error;
225 uma_zfree(pbuf_zone, pbuf);
227 free(pages, M_DEVBUF);