xref: /freebsd/sys/kern/kern_physio.c (revision a0ee8cc6)
1 /*-
2  * Copyright (c) 1994 John S. Dyson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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
15  *    John S. Dyson.
16  * 4. Modifications may be freely made to this file if the above conditions
17  *    are met.
18  */
19 
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/bio.h>
26 #include <sys/buf.h>
27 #include <sys/conf.h>
28 #include <sys/malloc.h>
29 #include <sys/proc.h>
30 #include <sys/uio.h>
31 #include <geom/geom.h>
32 
33 #include <vm/vm.h>
34 #include <vm/vm_page.h>
35 #include <vm/vm_extern.h>
36 #include <vm/vm_map.h>
37 
38 int
39 physio(struct cdev *dev, struct uio *uio, int ioflag)
40 {
41 	struct cdevsw *csw;
42 	struct buf *pbuf;
43 	struct bio *bp;
44 	struct vm_page **pages;
45 	caddr_t sa;
46 	u_int iolen, poff;
47 	int error, i, npages, maxpages;
48 	vm_prot_t prot;
49 
50 	csw = dev->si_devsw;
51 	/* check if character device is being destroyed */
52 	if (csw == NULL)
53 		return (ENXIO);
54 
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;
60 	}
61 
62 	/*
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.
65 	 */
66 	if (dev->si_flags & SI_NOSPLIT &&
67 	    (uio->uio_resid > dev->si_iosize_max || uio->uio_resid > MAXPHYS ||
68 	    uio->uio_iovcnt > 1)) {
69 		/*
70 		 * Tell the user why his I/O was rejected.
71 		 */
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),
83 			    uio->uio_iovcnt);
84 		return (EFBIG);
85 	}
86 
87 	/*
88 	 * Keep the process UPAGES from being swapped.  Processes swapped
89 	 * out while holding pbufs, used by swapper, may lead to deadlock.
90 	 */
91 	PHOLD(curproc);
92 
93 	bp = g_alloc_bio();
94 	if (uio->uio_segflg != UIO_USERSPACE) {
95 		pbuf = NULL;
96 		pages = NULL;
97 	} else if ((dev->si_flags & SI_UNMAPPED) && unmapped_buf_allowed) {
98 		pbuf = NULL;
99 		maxpages = btoc(MIN(uio->uio_resid, MAXPHYS)) + 1;
100 		pages = malloc(sizeof(*pages) * maxpages, M_DEVBUF, M_WAITOK);
101 	} else {
102 		pbuf = getpbuf(NULL);
103 		sa = pbuf->b_data;
104 		maxpages = btoc(MAXPHYS);
105 		pages = pbuf->b_pages;
106 	}
107 	prot = VM_PROT_READ;
108 	if (uio->uio_rw == UIO_READ)
109 		prot |= VM_PROT_WRITE;	/* Less backwards than it looks */
110 	error = 0;
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++;
117 			} else {
118 				bp->bio_cmd = BIO_WRITE;
119 				curthread->td_ru.ru_oublock++;
120 			}
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;
128 
129 			/*
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.
134 			 */
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),
141 					    bp->bio_data);
142 					error = EFBIG;
143 					goto doerror;
144 				}
145 				bp->bio_length = pbuf->b_kvasize;
146 				if (poff != 0)
147 					bp->bio_length -= PAGE_SIZE;
148 			}
149 
150 			bp->bio_bcount = bp->bio_length;
151 			bp->bio_dev = dev;
152 
153 			if (pages) {
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) {
158 					error = EFAULT;
159 					goto doerror;
160 				}
161 				if (pbuf) {
162 					pmap_qenter((vm_offset_t)sa,
163 					    pages, npages);
164 					bp->bio_data = sa + poff;
165 				} else {
166 					bp->bio_ma = pages;
167 					bp->bio_ma_n = npages;
168 					bp->bio_ma_offset = poff;
169 					bp->bio_data = unmapped_buf;
170 					bp->bio_flags |= BIO_UNMAPPED;
171 				}
172 			}
173 
174 			csw->d_strategy(bp);
175 			if (uio->uio_rw == UIO_READ)
176 				biowait(bp, "physrd");
177 			else
178 				biowait(bp, "physwr");
179 
180 			if (pages) {
181 				if (pbuf)
182 					pmap_qremove((vm_offset_t)sa, npages);
183 				vm_page_unhold_pages(pages, npages);
184 			}
185 
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;
196 				goto doerror;
197 			}
198 		}
199 	}
200 doerror:
201 	if (pbuf)
202 		relpbuf(pbuf, NULL);
203 	else if (pages)
204 		free(pages, M_DEVBUF);
205 	g_destroy_bio(bp);
206 	PRELE(curproc);
207 	return (error);
208 }
209