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/cdefs.h>
21 #include <sys/param.h>
22 #include <sys/systm.h>
26 #include <sys/malloc.h>
28 #include <sys/racct.h>
29 #include <sys/rwlock.h>
31 #include <geom/geom.h>
34 #include <vm/vm_object.h>
35 #include <vm/vm_page.h>
36 #include <vm/vm_pager.h>
37 #include <vm/vm_extern.h>
38 #include <vm/vm_map.h>
41 physio(struct cdev *dev, struct uio *uio, int ioflag)
46 struct vm_page **pages;
49 int error, i, npages, maxpages;
55 /* check if character device is being destroyed */
59 /* XXX: sanity check */
60 if (dev->si_iosize_max < PAGE_SIZE) {
61 printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
62 devtoname(dev), dev->si_iosize_max);
63 dev->si_iosize_max = DFLTPHYS;
67 * If the driver does not want I/O to be split, that means that we
68 * need to reject any requests that will not fit into one buffer.
70 if (dev->si_flags & SI_NOSPLIT &&
71 (uio->uio_resid > dev->si_iosize_max || uio->uio_resid > maxphys ||
72 uio->uio_iovcnt > 1)) {
74 * Tell the user why his I/O was rejected.
76 if (uio->uio_resid > dev->si_iosize_max)
77 uprintf("%s: request size=%zd > si_iosize_max=%d; "
78 "cannot split request\n", devtoname(dev),
79 uio->uio_resid, dev->si_iosize_max);
80 if (uio->uio_resid > maxphys)
81 uprintf("%s: request size=%zd > maxphys=%lu; "
82 "cannot split request\n", devtoname(dev),
83 uio->uio_resid, maxphys);
84 if (uio->uio_iovcnt > 1)
85 uprintf("%s: request vectors=%d > 1; "
86 "cannot split request\n", devtoname(dev),
92 * Keep the process UPAGES from being swapped. Processes swapped
93 * out while holding pbufs, used by swapper, may lead to deadlock.
98 if (uio->uio_segflg != UIO_USERSPACE) {
101 } else if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) {
103 maxpages = btoc(MIN(uio->uio_resid, maxphys)) + 1;
104 pages = malloc(sizeof(*pages) * maxpages, M_DEVBUF, M_WAITOK);
106 pbuf = uma_zalloc(pbuf_zone, M_WAITOK);
107 MPASS((pbuf->b_flags & B_MAXPHYS) != 0);
109 maxpages = PBUF_PAGES;
110 pages = pbuf->b_pages;
113 if (uio->uio_rw == UIO_READ)
114 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
116 for (i = 0; i < uio->uio_iovcnt; i++) {
120 if (uio->uio_rw == UIO_READ) {
121 racct_add_force(curproc, RACCT_READBPS,
122 uio->uio_iov[i].iov_len);
123 racct_add_force(curproc, RACCT_READIOPS, 1);
125 racct_add_force(curproc, RACCT_WRITEBPS,
126 uio->uio_iov[i].iov_len);
127 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
129 PROC_UNLOCK(curproc);
133 while (uio->uio_iov[i].iov_len) {
135 if (uio->uio_rw == UIO_READ) {
136 bp->bio_cmd = BIO_READ;
137 curthread->td_ru.ru_inblock++;
139 bp->bio_cmd = BIO_WRITE;
140 curthread->td_ru.ru_oublock++;
142 bp->bio_offset = uio->uio_offset;
143 base = uio->uio_iov[i].iov_base;
144 bp->bio_length = uio->uio_iov[i].iov_len;
145 if (bp->bio_length > dev->si_iosize_max)
146 bp->bio_length = dev->si_iosize_max;
147 if (bp->bio_length > maxphys)
148 bp->bio_length = maxphys;
149 bp->bio_bcount = bp->bio_length;
153 if ((npages = vm_fault_quick_hold_pages(
154 &curproc->p_vmspace->vm_map,
155 (vm_offset_t)base, bp->bio_length,
156 prot, pages, maxpages)) < 0) {
160 poff = (vm_offset_t)base & PAGE_MASK;
162 pmap_qenter((vm_offset_t)sa,
164 bp->bio_data = sa + poff;
167 bp->bio_ma_n = npages;
168 bp->bio_ma_offset = poff;
169 bp->bio_data = unmapped_buf;
170 bp->bio_flags |= BIO_UNMAPPED;
176 if (uio->uio_rw == UIO_READ)
177 biowait(bp, "physrd");
179 biowait(bp, "physwr");
183 pmap_qremove((vm_offset_t)sa, npages);
184 vm_page_unhold_pages(pages, npages);
187 iolen = bp->bio_length - bp->bio_resid;
188 if (iolen == 0 && !(bp->bio_flags & BIO_ERROR))
189 goto doerror; /* EOF */
190 uio->uio_iov[i].iov_len -= iolen;
191 uio->uio_iov[i].iov_base =
192 (char *)uio->uio_iov[i].iov_base + iolen;
193 uio->uio_resid -= iolen;
194 uio->uio_offset += iolen;
195 if (bp->bio_flags & BIO_ERROR) {
196 error = bp->bio_error;
203 uma_zfree(pbuf_zone, pbuf);
205 free(pages, M_DEVBUF);