xref: /freebsd/sys/kern/kern_physio.c (revision 64db83a8ab2d1f72a9b2174b39d2ef42b5b0580c)
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  * $FreeBSD$
20  */
21 
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/bio.h>
25 #include <sys/buf.h>
26 #include <sys/conf.h>
27 #include <sys/proc.h>
28 #include <sys/uio.h>
29 
30 #include <vm/vm.h>
31 #include <vm/vm_extern.h>
32 
33 static void
34 physwakeup(struct buf *bp)
35 {
36 	wakeup((caddr_t) bp);
37 }
38 
39 int
40 physio(dev_t dev, struct uio *uio, int ioflag)
41 {
42 	int i;
43 	int error;
44 	int spl;
45 	caddr_t sa;
46 	off_t blockno;
47 	u_int iolen;
48 	struct buf *bp;
49 
50 	/* Keep the process UPAGES from being swapped. XXX: why ? */
51 	PHOLD(curproc);
52 
53 	bp = getpbuf(NULL);
54 	sa = bp->b_data;
55 	error = bp->b_error = 0;
56 
57 	/* XXX: sanity check */
58 	if(dev->si_iosize_max < PAGE_SIZE) {
59 		printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
60 		    devtoname(dev), dev->si_iosize_max);
61 		dev->si_iosize_max = DFLTPHYS;
62 	}
63 
64 	for (i = 0; i < uio->uio_iovcnt; i++) {
65 		while (uio->uio_iov[i].iov_len) {
66 			bp->b_flags = B_PHYS;
67 			if (uio->uio_rw == UIO_READ)
68 				bp->b_iocmd = BIO_READ;
69 			else
70 				bp->b_iocmd = BIO_WRITE;
71 			bp->b_dev = dev;
72 			bp->b_iodone = physwakeup;
73 			bp->b_data = uio->uio_iov[i].iov_base;
74 			bp->b_bcount = uio->uio_iov[i].iov_len;
75 			bp->b_offset = uio->uio_offset;
76 			bp->b_saveaddr = sa;
77 
78 			/* Don't exceed drivers iosize limit */
79 			if (bp->b_bcount > dev->si_iosize_max)
80 				bp->b_bcount = dev->si_iosize_max;
81 
82 			/*
83 			 * Make sure the pbuf can map the request
84 			 * XXX: The pbuf has kvasize = MAXPHYS so a request
85 			 * XXX: larger than MAXPHYS - PAGE_SIZE must be
86 			 * XXX: page aligned or it will be fragmented.
87 			 */
88 			iolen = ((vm_offset_t) bp->b_data) & PAGE_MASK;
89 			if ((bp->b_bcount + iolen) > bp->b_kvasize) {
90 				bp->b_bcount = bp->b_kvasize;
91 				if (iolen != 0)
92 					bp->b_bcount -= PAGE_SIZE;
93 			}
94 			bp->b_bufsize = bp->b_bcount;
95 
96 			blockno = bp->b_offset >> DEV_BSHIFT;
97 			if ((daddr_t)blockno != blockno) {
98 				error = EINVAL; /* blockno overflow */
99 				goto doerror;
100 			}
101 			bp->b_blkno = blockno;
102 
103 			if (uio->uio_segflg == UIO_USERSPACE) {
104 				if (!useracc(bp->b_data, bp->b_bufsize,
105 				    bp->b_iocmd == BIO_READ ?
106 				    VM_PROT_WRITE : VM_PROT_READ)) {
107 					error = EFAULT;
108 					goto doerror;
109 				}
110 				vmapbuf(bp);
111 			}
112 
113 			DEV_STRATEGY(bp, 0);
114 			spl = splbio();
115 			while ((bp->b_flags & B_DONE) == 0)
116 				tsleep((caddr_t)bp, PRIBIO, "physstr", 0);
117 			splx(spl);
118 
119 			if (uio->uio_segflg == UIO_USERSPACE)
120 				vunmapbuf(bp);
121 			iolen = bp->b_bcount - bp->b_resid;
122 			if (iolen == 0 && !(bp->b_ioflags & BIO_ERROR))
123 				goto doerror;	/* EOF */
124 			uio->uio_iov[i].iov_len -= iolen;
125 			uio->uio_iov[i].iov_base += iolen;
126 			uio->uio_resid -= iolen;
127 			uio->uio_offset += iolen;
128 			if( bp->b_ioflags & BIO_ERROR) {
129 				error = bp->b_error;
130 				goto doerror;
131 			}
132 		}
133 	}
134 doerror:
135 	relpbuf(bp, NULL);
136 	PRELE(curproc);
137 	return (error);
138 }
139