30#include <sys/malloc.h>
33#include <sys/rwlock.h>
38#include <vm/vm_object.h>
39#include <vm/vm_page.h>
40#include <vm/vm_pager.h>
41#include <vm/vm_extern.h>
45physio(
struct cdev *dev,
struct uio *uio,
int ioflag)
50 struct vm_page **pages;
53 int error, i, npages, maxpages;
64 if (dev->si_iosize_max < PAGE_SIZE) {
65 printf(
"WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
67 dev->si_iosize_max = DFLTPHYS;
74 if (dev->si_flags & SI_NOSPLIT &&
75 (uio->uio_resid > dev->si_iosize_max || uio->uio_resid >
maxphys ||
76 uio->uio_iovcnt > 1)) {
80 if (uio->uio_resid > dev->si_iosize_max)
81 uprintf(
"%s: request size=%zd > si_iosize_max=%d; "
83 uio->uio_resid, dev->si_iosize_max);
85 uprintf(
"%s: request size=%zd > maxphys=%lu; "
88 if (uio->uio_iovcnt > 1)
89 uprintf(
"%s: request vectors=%d > 1; "
102 if (uio->uio_segflg != UIO_USERSPACE) {
105 }
else if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) {
107 maxpages = btoc(MIN(uio->uio_resid,
maxphys)) + 1;
108 pages =
malloc(
sizeof(*pages) * maxpages, M_DEVBUF, M_WAITOK);
110 pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
111 MPASS((pbuf->b_flags & B_MAXPHYS) != 0);
113 maxpages = PBUF_PAGES;
114 pages = pbuf->b_pages;
117 if (uio->uio_rw == UIO_READ)
118 prot |= VM_PROT_WRITE;
120 for (i = 0; i < uio->uio_iovcnt; i++) {
124 if (uio->uio_rw == UIO_READ) {
125 racct_add_force(curproc, RACCT_READBPS,
126 uio->uio_iov[i].iov_len);
127 racct_add_force(curproc, RACCT_READIOPS, 1);
129 racct_add_force(curproc, RACCT_WRITEBPS,
130 uio->uio_iov[i].iov_len);
131 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
133 PROC_UNLOCK(curproc);
137 while (uio->uio_iov[i].iov_len) {
139 if (uio->uio_rw == UIO_READ) {
140 bp->bio_cmd = BIO_READ;
141 curthread->td_ru.ru_inblock++;
143 bp->bio_cmd = BIO_WRITE;
144 curthread->td_ru.ru_oublock++;
146 bp->bio_offset = uio->uio_offset;
147 base = uio->uio_iov[i].iov_base;
148 bp->bio_length = uio->uio_iov[i].iov_len;
149 if (bp->bio_length > dev->si_iosize_max)
150 bp->bio_length = dev->si_iosize_max;
153 bp->bio_bcount = bp->bio_length;
157 if ((npages = vm_fault_quick_hold_pages(
158 &curproc->p_vmspace->vm_map,
159 (vm_offset_t)base, bp->bio_length,
160 prot, pages, maxpages)) < 0) {
164 poff = (vm_offset_t)base & PAGE_MASK;
166 pmap_qenter((vm_offset_t)sa,
168 bp->bio_data = sa + poff;
171 bp->bio_ma_n = npages;
172 bp->bio_ma_offset = poff;
174 bp->bio_flags |= BIO_UNMAPPED;
180 if (uio->uio_rw == UIO_READ)
187 pmap_qremove((vm_offset_t)sa, npages);
188 vm_page_unhold_pages(pages, npages);
191 iolen = bp->bio_length - bp->bio_resid;
192 if (iolen == 0 && !(bp->bio_flags & BIO_ERROR))
194 uio->uio_iov[i].iov_len -= iolen;
195 uio->uio_iov[i].iov_base =
196 (
char *)uio->uio_iov[i].iov_base + iolen;
197 uio->uio_resid -= iolen;
198 uio->uio_offset += iolen;
199 if (bp->bio_flags & BIO_ERROR) {
200 error = bp->bio_error;
207 uma_zfree(pbuf_zone, pbuf);
209 free(pages, M_DEVBUF);
const char * devtoname(struct cdev *dev)
void *() malloc(size_t size, struct malloc_type *mtp, int flags)
void free(void *addr, struct malloc_type *mtp)
int physio(struct cdev *dev, struct uio *uio, int ioflag)
int printf(const char *fmt,...)
int uprintf(const char *fmt,...)
caddr_t __read_mostly unmapped_buf
int biowait(struct bio *bp, const char *wmesg)