2 * Copyright (c) 1994 John S. Dyson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * 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 * 3. Absolutely no warranty of function or purpose is made by the author
16 * 4. Modifications may be freely made to this file if the above conditions
20 #include <sys/param.h>
21 #include <sys/systm.h>
25 #include <sys/malloc.h>
27 #include <sys/racct.h>
28 #include <sys/rwlock.h>
30 #include <geom/geom.h>
33 #include <vm/vm_object.h>
34 #include <vm/vm_page.h>
35 #include <vm/vm_pager.h>
36 #include <vm/vm_extern.h>
37 #include <vm/vm_map.h>
40 physio(struct cdev *dev, struct uio *uio, int ioflag)
45 struct vm_page **pages;
48 int error, i, npages, maxpages;
54 /* check if character device is being destroyed */
58 /* XXX: sanity check */
59 if (dev->si_iosize_max < PAGE_SIZE) {
60 printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
61 devtoname(dev), dev->si_iosize_max);
62 dev->si_iosize_max = DFLTPHYS;
66 * If the driver does not want I/O to be split, that means that we
67 * need to reject any requests that will not fit into one buffer.
69 if (dev->si_flags & SI_NOSPLIT &&
70 (uio->uio_resid > dev->si_iosize_max || uio->uio_resid > maxphys ||
71 uio->uio_iovcnt > 1)) {
73 * Tell the user why his I/O was rejected.
75 if (uio->uio_resid > dev->si_iosize_max)
76 uprintf("%s: request size=%zd > si_iosize_max=%d; "
77 "cannot split request\n", devtoname(dev),
78 uio->uio_resid, dev->si_iosize_max);
79 if (uio->uio_resid > maxphys)
80 uprintf("%s: request size=%zd > maxphys=%lu; "
81 "cannot split request\n", devtoname(dev),
82 uio->uio_resid, maxphys);
83 if (uio->uio_iovcnt > 1)
84 uprintf("%s: request vectors=%d > 1; "
85 "cannot split request\n", devtoname(dev),
91 * Keep the process UPAGES from being swapped. Processes swapped
92 * out while holding pbufs, used by swapper, may lead to deadlock.
97 if (uio->uio_segflg != UIO_USERSPACE) {
100 } else if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) {
102 maxpages = btoc(MIN(uio->uio_resid, maxphys)) + 1;
103 pages = malloc(sizeof(*pages) * maxpages, M_DEVBUF, M_WAITOK);
105 pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
106 MPASS((pbuf->b_flags & B_MAXPHYS) != 0);
108 maxpages = PBUF_PAGES;
109 pages = pbuf->b_pages;
112 if (uio->uio_rw == UIO_READ)
113 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
115 for (i = 0; i < uio->uio_iovcnt; i++) {
119 if (uio->uio_rw == UIO_READ) {
120 racct_add_force(curproc, RACCT_READBPS,
121 uio->uio_iov[i].iov_len);
122 racct_add_force(curproc, RACCT_READIOPS, 1);
124 racct_add_force(curproc, RACCT_WRITEBPS,
125 uio->uio_iov[i].iov_len);
126 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
128 PROC_UNLOCK(curproc);
132 while (uio->uio_iov[i].iov_len) {
134 if (uio->uio_rw == UIO_READ) {
135 bp->bio_cmd = BIO_READ;
136 curthread->td_ru.ru_inblock++;
138 bp->bio_cmd = BIO_WRITE;
139 curthread->td_ru.ru_oublock++;
141 bp->bio_offset = uio->uio_offset;
142 base = uio->uio_iov[i].iov_base;
143 bp->bio_length = uio->uio_iov[i].iov_len;
144 if (bp->bio_length > dev->si_iosize_max)
145 bp->bio_length = dev->si_iosize_max;
146 if (bp->bio_length > maxphys)
147 bp->bio_length = maxphys;
148 bp->bio_bcount = bp->bio_length;
152 if ((npages = vm_fault_quick_hold_pages(
153 &curproc->p_vmspace->vm_map,
154 (vm_offset_t)base, bp->bio_length,
155 prot, pages, maxpages)) < 0) {
159 poff = (vm_offset_t)base & PAGE_MASK;
161 pmap_qenter((vm_offset_t)sa,
163 bp->bio_data = sa + poff;
166 bp->bio_ma_n = npages;
167 bp->bio_ma_offset = poff;
168 bp->bio_data = unmapped_buf;
169 bp->bio_flags |= BIO_UNMAPPED;
175 if (uio->uio_rw == UIO_READ)
176 biowait(bp, "physrd");
178 biowait(bp, "physwr");
182 pmap_qremove((vm_offset_t)sa, npages);
183 vm_page_unhold_pages(pages, npages);
186 iolen = bp->bio_length - bp->bio_resid;
187 if (iolen == 0 && !(bp->bio_flags & BIO_ERROR))
188 goto doerror; /* EOF */
189 uio->uio_iov[i].iov_len -= iolen;
190 uio->uio_iov[i].iov_base =
191 (char *)uio->uio_iov[i].iov_base + iolen;
192 uio->uio_resid -= iolen;
193 uio->uio_offset += iolen;
194 if (bp->bio_flags & BIO_ERROR) {
195 error = bp->bio_error;
202 uma_zfree(pbuf_zone, pbuf);
204 free(pages, M_DEVBUF);