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>
31 #include <geom/geom.h>
34 #include <vm/vm_page.h>
35 #include <vm/vm_extern.h>
36 #include <vm/vm_map.h>
39 physio(struct cdev *dev, struct uio *uio, int ioflag)
44 struct vm_page **pages;
47 int error, i, npages, maxpages;
51 /* check if character device is being destroyed */
55 /* XXX: sanity check */
56 if(dev->si_iosize_max < PAGE_SIZE) {
57 printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
58 devtoname(dev), dev->si_iosize_max);
59 dev->si_iosize_max = DFLTPHYS;
63 * If the driver does not want I/O to be split, that means that we
64 * need to reject any requests that will not fit into one buffer.
66 if (dev->si_flags & SI_NOSPLIT &&
67 (uio->uio_resid > dev->si_iosize_max || uio->uio_resid > MAXPHYS ||
68 uio->uio_iovcnt > 1)) {
70 * Tell the user why his I/O was rejected.
72 if (uio->uio_resid > dev->si_iosize_max)
73 uprintf("%s: request size=%zd > si_iosize_max=%d; "
74 "cannot split request\n", devtoname(dev),
75 uio->uio_resid, dev->si_iosize_max);
76 if (uio->uio_resid > MAXPHYS)
77 uprintf("%s: request size=%zd > MAXPHYS=%d; "
78 "cannot split request\n", devtoname(dev),
79 uio->uio_resid, MAXPHYS);
80 if (uio->uio_iovcnt > 1)
81 uprintf("%s: request vectors=%d > 1; "
82 "cannot split request\n", devtoname(dev),
88 * Keep the process UPAGES from being swapped. Processes swapped
89 * out while holding pbufs, used by swapper, may lead to deadlock.
94 if (uio->uio_segflg != UIO_USERSPACE) {
97 } else if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) {
99 maxpages = btoc(MIN(uio->uio_resid, MAXPHYS)) + 1;
100 pages = malloc(sizeof(*pages) * maxpages, M_DEVBUF, M_WAITOK);
102 pbuf = getpbuf(NULL);
104 maxpages = btoc(MAXPHYS);
105 pages = pbuf->b_pages;
108 if (uio->uio_rw == UIO_READ)
109 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
111 for (i = 0; i < uio->uio_iovcnt; i++) {
112 while (uio->uio_iov[i].iov_len) {
113 bzero(bp, sizeof(*bp));
114 if (uio->uio_rw == UIO_READ) {
115 bp->bio_cmd = BIO_READ;
116 curthread->td_ru.ru_inblock++;
118 bp->bio_cmd = BIO_WRITE;
119 curthread->td_ru.ru_oublock++;
121 bp->bio_offset = uio->uio_offset;
122 bp->bio_data = uio->uio_iov[i].iov_base;
123 bp->bio_length = uio->uio_iov[i].iov_len;
124 if (bp->bio_length > dev->si_iosize_max)
125 bp->bio_length = dev->si_iosize_max;
126 if (bp->bio_length > MAXPHYS)
127 bp->bio_length = MAXPHYS;
130 * Make sure the pbuf can map the request.
131 * The pbuf has kvasize = MAXPHYS, so a request
132 * larger than MAXPHYS - PAGE_SIZE must be
133 * page aligned or it will be fragmented.
135 poff = (vm_offset_t)bp->bio_data & PAGE_MASK;
136 if (pbuf && bp->bio_length + poff > pbuf->b_kvasize) {
137 if (dev->si_flags & SI_NOSPLIT) {
138 uprintf("%s: request ptr %p is not "
139 "on a page boundary; cannot split "
140 "request\n", devtoname(dev),
145 bp->bio_length = pbuf->b_kvasize;
147 bp->bio_length -= PAGE_SIZE;
150 bp->bio_bcount = bp->bio_length;
154 if ((npages = vm_fault_quick_hold_pages(
155 &curproc->p_vmspace->vm_map,
156 (vm_offset_t)bp->bio_data, bp->bio_length,
157 prot, pages, maxpages)) < 0) {
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;
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;
204 free(pages, M_DEVBUF);