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>
33 #include <sys/rwlock.h>
35 #include <geom/geom.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>
42 #include <vm/vm_map.h>
45 physio(struct cdev *dev, struct uio *uio, int ioflag)
50 struct vm_page **pages;
53 int error, i, npages, maxpages;
59 /* check if character device is being destroyed */
63 /* XXX: sanity check */
64 if(dev->si_iosize_max < PAGE_SIZE) {
65 printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
66 devtoname(dev), dev->si_iosize_max);
67 dev->si_iosize_max = DFLTPHYS;
71 * If the driver does not want I/O to be split, that means that we
72 * need to reject any requests that will not fit into one buffer.
74 if (dev->si_flags & SI_NOSPLIT &&
75 (uio->uio_resid > dev->si_iosize_max || uio->uio_resid > maxphys ||
76 uio->uio_iovcnt > 1)) {
78 * Tell the user why his I/O was rejected.
80 if (uio->uio_resid > dev->si_iosize_max)
81 uprintf("%s: request size=%zd > si_iosize_max=%d; "
82 "cannot split request\n", devtoname(dev),
83 uio->uio_resid, dev->si_iosize_max);
84 if (uio->uio_resid > maxphys)
85 uprintf("%s: request size=%zd > maxphys=%lu; "
86 "cannot split request\n", devtoname(dev),
87 uio->uio_resid, maxphys);
88 if (uio->uio_iovcnt > 1)
89 uprintf("%s: request vectors=%d > 1; "
90 "cannot split request\n", devtoname(dev),
96 * Keep the process UPAGES from being swapped. Processes swapped
97 * out while holding pbufs, used by swapper, may lead to deadlock.
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; /* Less backwards than it looks */
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;
151 if (bp->bio_length > maxphys)
152 bp->bio_length = maxphys;
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;
173 bp->bio_data = unmapped_buf;
174 bp->bio_flags |= BIO_UNMAPPED;
180 if (uio->uio_rw == UIO_READ)
181 biowait(bp, "physrd");
183 biowait(bp, "physwr");
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))
193 goto doerror; /* EOF */
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);