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 __FBSDID("$FreeBSD$");
23 #include <sys/param.h>
24 #include <sys/systm.h>
28 #include <sys/malloc.h>
30 #include <sys/racct.h>
32 #include <geom/geom.h>
35 #include <vm/vm_page.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;
52 /* check if character device is being destroyed */
56 /* XXX: sanity check */
57 if(dev->si_iosize_max < PAGE_SIZE) {
58 printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
59 devtoname(dev), dev->si_iosize_max);
60 dev->si_iosize_max = DFLTPHYS;
64 * If the driver does not want I/O to be split, that means that we
65 * need to reject any requests that will not fit into one buffer.
67 if (dev->si_flags & SI_NOSPLIT &&
68 (uio->uio_resid > dev->si_iosize_max || uio->uio_resid > MAXPHYS ||
69 uio->uio_iovcnt > 1)) {
71 * Tell the user why his I/O was rejected.
73 if (uio->uio_resid > dev->si_iosize_max)
74 uprintf("%s: request size=%zd > si_iosize_max=%d; "
75 "cannot split request\n", devtoname(dev),
76 uio->uio_resid, dev->si_iosize_max);
77 if (uio->uio_resid > MAXPHYS)
78 uprintf("%s: request size=%zd > MAXPHYS=%d; "
79 "cannot split request\n", devtoname(dev),
80 uio->uio_resid, MAXPHYS);
81 if (uio->uio_iovcnt > 1)
82 uprintf("%s: request vectors=%d > 1; "
83 "cannot split request\n", devtoname(dev),
89 * Keep the process UPAGES from being swapped. Processes swapped
90 * out while holding pbufs, used by swapper, may lead to deadlock.
95 if (uio->uio_segflg != UIO_USERSPACE) {
98 } else if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) {
100 maxpages = btoc(MIN(uio->uio_resid, MAXPHYS)) + 1;
101 pages = malloc(sizeof(*pages) * maxpages, M_DEVBUF, M_WAITOK);
103 pbuf = getpbuf(NULL);
105 maxpages = btoc(MAXPHYS);
106 pages = pbuf->b_pages;
109 if (uio->uio_rw == UIO_READ)
110 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
112 for (i = 0; i < uio->uio_iovcnt; i++) {
116 if (uio->uio_rw == UIO_READ) {
117 racct_add_force(curproc, RACCT_READBPS,
118 uio->uio_iov[i].iov_len);
119 racct_add_force(curproc, RACCT_READIOPS, 1);
121 racct_add_force(curproc, RACCT_WRITEBPS,
122 uio->uio_iov[i].iov_len);
123 racct_add_force(curproc, RACCT_WRITEIOPS, 1);
125 PROC_UNLOCK(curproc);
129 while (uio->uio_iov[i].iov_len) {
131 if (uio->uio_rw == UIO_READ) {
132 bp->bio_cmd = BIO_READ;
133 curthread->td_ru.ru_inblock++;
135 bp->bio_cmd = BIO_WRITE;
136 curthread->td_ru.ru_oublock++;
138 bp->bio_offset = uio->uio_offset;
139 bp->bio_data = uio->uio_iov[i].iov_base;
140 bp->bio_length = uio->uio_iov[i].iov_len;
141 if (bp->bio_length > dev->si_iosize_max)
142 bp->bio_length = dev->si_iosize_max;
143 if (bp->bio_length > MAXPHYS)
144 bp->bio_length = MAXPHYS;
147 * Make sure the pbuf can map the request.
148 * The pbuf has kvasize = MAXPHYS, so a request
149 * larger than MAXPHYS - PAGE_SIZE must be
150 * page aligned or it will be fragmented.
152 poff = (vm_offset_t)bp->bio_data & PAGE_MASK;
153 if (pbuf && bp->bio_length + poff > pbuf->b_kvasize) {
154 if (dev->si_flags & SI_NOSPLIT) {
155 uprintf("%s: request ptr %p is not "
156 "on a page boundary; cannot split "
157 "request\n", devtoname(dev),
162 bp->bio_length = pbuf->b_kvasize;
164 bp->bio_length -= PAGE_SIZE;
167 bp->bio_bcount = bp->bio_length;
171 if ((npages = vm_fault_quick_hold_pages(
172 &curproc->p_vmspace->vm_map,
173 (vm_offset_t)bp->bio_data, bp->bio_length,
174 prot, pages, maxpages)) < 0) {
179 pmap_qenter((vm_offset_t)sa,
181 bp->bio_data = sa + poff;
184 bp->bio_ma_n = npages;
185 bp->bio_ma_offset = poff;
186 bp->bio_data = unmapped_buf;
187 bp->bio_flags |= BIO_UNMAPPED;
192 if (uio->uio_rw == UIO_READ)
193 biowait(bp, "physrd");
195 biowait(bp, "physwr");
199 pmap_qremove((vm_offset_t)sa, npages);
200 vm_page_unhold_pages(pages, npages);
203 iolen = bp->bio_length - bp->bio_resid;
204 if (iolen == 0 && !(bp->bio_flags & BIO_ERROR))
205 goto doerror; /* EOF */
206 uio->uio_iov[i].iov_len -= iolen;
207 uio->uio_iov[i].iov_base =
208 (char *)uio->uio_iov[i].iov_base + iolen;
209 uio->uio_resid -= iolen;
210 uio->uio_offset += iolen;
211 if (bp->bio_flags & BIO_ERROR) {
212 error = bp->bio_error;
221 free(pages, M_DEVBUF);