xref: /freebsd/sys/kern/kern_physio.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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/proc.h>
29 #include <sys/uio.h>
30 
31 #include <vm/vm.h>
32 #include <vm/vm_extern.h>
33 
34 int
35 physio(struct cdev *dev, struct uio *uio, int ioflag)
36 {
37 	int i;
38 	int error;
39 	caddr_t sa;
40 	u_int iolen;
41 	struct buf *bp;
42 
43 	/* Keep the process UPAGES from being swapped. XXX: why ? */
44 	PHOLD(curproc);
45 
46 	bp = getpbuf(NULL);
47 	sa = bp->b_data;
48 	error = 0;
49 
50 	/* XXX: sanity check */
51 	if(dev->si_iosize_max < PAGE_SIZE) {
52 		printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
53 		    devtoname(dev), dev->si_iosize_max);
54 		dev->si_iosize_max = DFLTPHYS;
55 	}
56 
57 	for (i = 0; i < uio->uio_iovcnt; i++) {
58 		while (uio->uio_iov[i].iov_len) {
59 			bp->b_flags = 0;
60 			if (uio->uio_rw == UIO_READ)
61 				bp->b_iocmd = BIO_READ;
62 			else
63 				bp->b_iocmd = BIO_WRITE;
64 			bp->b_iodone = bdone;
65 			bp->b_data = uio->uio_iov[i].iov_base;
66 			bp->b_bcount = uio->uio_iov[i].iov_len;
67 			bp->b_offset = uio->uio_offset;
68 			bp->b_iooffset = uio->uio_offset;
69 			bp->b_saveaddr = sa;
70 
71 			/* Don't exceed drivers iosize limit */
72 			if (bp->b_bcount > dev->si_iosize_max)
73 				bp->b_bcount = dev->si_iosize_max;
74 
75 			/*
76 			 * Make sure the pbuf can map the request
77 			 * XXX: The pbuf has kvasize = MAXPHYS so a request
78 			 * XXX: larger than MAXPHYS - PAGE_SIZE must be
79 			 * XXX: page aligned or it will be fragmented.
80 			 */
81 			iolen = ((vm_offset_t) bp->b_data) & PAGE_MASK;
82 			if ((bp->b_bcount + iolen) > bp->b_kvasize) {
83 				bp->b_bcount = bp->b_kvasize;
84 				if (iolen != 0)
85 					bp->b_bcount -= PAGE_SIZE;
86 			}
87 			bp->b_bufsize = bp->b_bcount;
88 
89 			bp->b_blkno = btodb(bp->b_offset);
90 
91 			if (uio->uio_segflg == UIO_USERSPACE)
92 				if (vmapbuf(bp) < 0) {
93 					error = EFAULT;
94 					goto doerror;
95 				}
96 
97 			dev_strategy(dev, bp);
98 			if (uio->uio_rw == UIO_READ)
99 				bwait(bp, PRIBIO, "physrd");
100 			else
101 				bwait(bp, PRIBIO, "physwr");
102 
103 			if (uio->uio_segflg == UIO_USERSPACE)
104 				vunmapbuf(bp);
105 			iolen = bp->b_bcount - bp->b_resid;
106 			if (iolen == 0 && !(bp->b_ioflags & BIO_ERROR))
107 				goto doerror;	/* EOF */
108 			uio->uio_iov[i].iov_len -= iolen;
109 			uio->uio_iov[i].iov_base =
110 			    (char *)uio->uio_iov[i].iov_base + iolen;
111 			uio->uio_resid -= iolen;
112 			uio->uio_offset += iolen;
113 			if( bp->b_ioflags & BIO_ERROR) {
114 				error = bp->b_error;
115 				goto doerror;
116 			}
117 		}
118 	}
119 doerror:
120 	relpbuf(bp, NULL);
121 	PRELE(curproc);
122 	return (error);
123 }
124