1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1994 John S. Dyson 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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 17 * John S. Dyson. 18 * 4. Modifications may be freely made to this file if the above conditions 19 * are met. 20 */ 21 22 #include <sys/cdefs.h> 23 __FBSDID("$FreeBSD$"); 24 25 #include <sys/param.h> 26 #include <sys/systm.h> 27 #include <sys/bio.h> 28 #include <sys/buf.h> 29 #include <sys/conf.h> 30 #include <sys/malloc.h> 31 #include <sys/proc.h> 32 #include <sys/racct.h> 33 #include <sys/uio.h> 34 #include <geom/geom.h> 35 36 #include <vm/vm.h> 37 #include <vm/vm_page.h> 38 #include <vm/vm_extern.h> 39 #include <vm/vm_map.h> 40 41 int 42 physio(struct cdev *dev, struct uio *uio, int ioflag) 43 { 44 struct cdevsw *csw; 45 struct buf *pbuf; 46 struct bio *bp; 47 struct vm_page **pages; 48 caddr_t sa; 49 u_int iolen, poff; 50 int error, i, npages, maxpages; 51 vm_prot_t prot; 52 53 csw = dev->si_devsw; 54 /* check if character device is being destroyed */ 55 if (csw == NULL) 56 return (ENXIO); 57 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; 63 } 64 65 /* 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. 68 */ 69 if (dev->si_flags & SI_NOSPLIT && 70 (uio->uio_resid > dev->si_iosize_max || uio->uio_resid > MAXPHYS || 71 uio->uio_iovcnt > 1)) { 72 /* 73 * Tell the user why his I/O was rejected. 74 */ 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=%d; " 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), 86 uio->uio_iovcnt); 87 return (EFBIG); 88 } 89 90 /* 91 * Keep the process UPAGES from being swapped. Processes swapped 92 * out while holding pbufs, used by swapper, may lead to deadlock. 93 */ 94 PHOLD(curproc); 95 96 bp = g_alloc_bio(); 97 if (uio->uio_segflg != UIO_USERSPACE) { 98 pbuf = NULL; 99 pages = NULL; 100 } else if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) { 101 pbuf = NULL; 102 maxpages = btoc(MIN(uio->uio_resid, MAXPHYS)) + 1; 103 pages = malloc(sizeof(*pages) * maxpages, M_DEVBUF, M_WAITOK); 104 } else { 105 pbuf = getpbuf(NULL); 106 sa = pbuf->b_data; 107 maxpages = btoc(MAXPHYS); 108 pages = pbuf->b_pages; 109 } 110 prot = VM_PROT_READ; 111 if (uio->uio_rw == UIO_READ) 112 prot |= VM_PROT_WRITE; /* Less backwards than it looks */ 113 error = 0; 114 for (i = 0; i < uio->uio_iovcnt; i++) { 115 #ifdef RACCT 116 if (racct_enable) { 117 PROC_LOCK(curproc); 118 if (uio->uio_rw == UIO_READ) { 119 racct_add_force(curproc, RACCT_READBPS, 120 uio->uio_iov[i].iov_len); 121 racct_add_force(curproc, RACCT_READIOPS, 1); 122 } else { 123 racct_add_force(curproc, RACCT_WRITEBPS, 124 uio->uio_iov[i].iov_len); 125 racct_add_force(curproc, RACCT_WRITEIOPS, 1); 126 } 127 PROC_UNLOCK(curproc); 128 } 129 #endif /* RACCT */ 130 131 while (uio->uio_iov[i].iov_len) { 132 g_reset_bio(bp); 133 if (uio->uio_rw == UIO_READ) { 134 bp->bio_cmd = BIO_READ; 135 curthread->td_ru.ru_inblock++; 136 } else { 137 bp->bio_cmd = BIO_WRITE; 138 curthread->td_ru.ru_oublock++; 139 } 140 bp->bio_offset = uio->uio_offset; 141 bp->bio_data = uio->uio_iov[i].iov_base; 142 bp->bio_length = uio->uio_iov[i].iov_len; 143 if (bp->bio_length > dev->si_iosize_max) 144 bp->bio_length = dev->si_iosize_max; 145 if (bp->bio_length > MAXPHYS) 146 bp->bio_length = MAXPHYS; 147 148 /* 149 * Make sure the pbuf can map the request. 150 * The pbuf has kvasize = MAXPHYS, so a request 151 * larger than MAXPHYS - PAGE_SIZE must be 152 * page aligned or it will be fragmented. 153 */ 154 poff = (vm_offset_t)bp->bio_data & PAGE_MASK; 155 if (pbuf && bp->bio_length + poff > pbuf->b_kvasize) { 156 if (dev->si_flags & SI_NOSPLIT) { 157 uprintf("%s: request ptr %p is not " 158 "on a page boundary; cannot split " 159 "request\n", devtoname(dev), 160 bp->bio_data); 161 error = EFBIG; 162 goto doerror; 163 } 164 bp->bio_length = pbuf->b_kvasize; 165 if (poff != 0) 166 bp->bio_length -= PAGE_SIZE; 167 } 168 169 bp->bio_bcount = bp->bio_length; 170 bp->bio_dev = dev; 171 172 if (pages) { 173 if ((npages = vm_fault_quick_hold_pages( 174 &curproc->p_vmspace->vm_map, 175 (vm_offset_t)bp->bio_data, bp->bio_length, 176 prot, pages, maxpages)) < 0) { 177 error = EFAULT; 178 goto doerror; 179 } 180 if (pbuf) { 181 pmap_qenter((vm_offset_t)sa, 182 pages, npages); 183 bp->bio_data = sa + poff; 184 } else { 185 bp->bio_ma = pages; 186 bp->bio_ma_n = npages; 187 bp->bio_ma_offset = poff; 188 bp->bio_data = unmapped_buf; 189 bp->bio_flags |= BIO_UNMAPPED; 190 } 191 } 192 193 csw->d_strategy(bp); 194 if (uio->uio_rw == UIO_READ) 195 biowait(bp, "physrd"); 196 else 197 biowait(bp, "physwr"); 198 199 if (pages) { 200 if (pbuf) 201 pmap_qremove((vm_offset_t)sa, npages); 202 vm_page_unhold_pages(pages, npages); 203 } 204 205 iolen = bp->bio_length - bp->bio_resid; 206 if (iolen == 0 && !(bp->bio_flags & BIO_ERROR)) 207 goto doerror; /* EOF */ 208 uio->uio_iov[i].iov_len -= iolen; 209 uio->uio_iov[i].iov_base = 210 (char *)uio->uio_iov[i].iov_base + iolen; 211 uio->uio_resid -= iolen; 212 uio->uio_offset += iolen; 213 if (bp->bio_flags & BIO_ERROR) { 214 error = bp->bio_error; 215 goto doerror; 216 } 217 } 218 } 219 doerror: 220 if (pbuf) 221 relpbuf(pbuf, NULL); 222 else if (pages) 223 free(pages, M_DEVBUF); 224 g_destroy_bio(bp); 225 PRELE(curproc); 226 return (error); 227 } 228