xref: /dragonfly/sys/kern/kern_physio.c (revision 1bf4b486)
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: src/sys/kern/kern_physio.c,v 1.46.2.4 2003/11/14 09:51:47 simokawa Exp $
20  * $DragonFly: src/sys/kern/kern_physio.c,v 1.10 2005/06/06 15:02:28 dillon Exp $
21  */
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/buf.h>
26 #include <sys/conf.h>
27 #include <sys/proc.h>
28 #include <sys/uio.h>
29 #include <sys/device.h>
30 
31 #include <vm/vm.h>
32 #include <vm/vm_extern.h>
33 
34 #include <machine/physio_proc.h>
35 
36 /* I have no idea what this is used for */
37 struct physio_proc_head physio_proc_freet, physio_proc_busyt;
38 
39 static void
40 physwakeup(struct buf *bp)
41 {
42 	wakeup((caddr_t) bp);
43 }
44 
45 int
46 physio(dev_t dev, struct uio *uio, int ioflag)
47 {
48 	int i;
49 	int error;
50 	int chk_blockno;
51 	caddr_t sa;
52 	off_t blockno;
53 	u_int iolen;
54 	struct buf *bp;
55 
56 	/*
57 	 * NOTE: we no longer have to PHOLD() the process, because the
58 	 * kernel stack / uarea cannot be swapped, and when it can be in
59 	 * the future it will only happen if the process is sleeping on
60 	 * a particular address.
61 	 */
62 	bp = getpbuf(NULL);
63 	sa = bp->b_data;
64 	error = 0;
65 
66 	/* XXX: sanity check */
67 	if(dev->si_iosize_max < PAGE_SIZE) {
68 		printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
69 		    devtoname(dev), dev->si_iosize_max);
70 		dev->si_iosize_max = DFLTPHYS;
71 	}
72 
73 	/* Don't check block number overflow for D_MEM */
74 	if ((dev_dflags(dev) & D_TYPEMASK) == D_MEM)
75 		chk_blockno = 0;
76 	else
77 		chk_blockno = 1;
78 
79 	for (i = 0; i < uio->uio_iovcnt; i++) {
80 		while (uio->uio_iov[i].iov_len) {
81 			if (uio->uio_rw == UIO_READ)
82 				bp->b_flags = B_PHYS | B_CALL | B_READ;
83 			else
84 				bp->b_flags = B_PHYS | B_CALL | B_WRITE;
85 			bp->b_dev = dev;
86 			bp->b_iodone = physwakeup;
87 			bp->b_data = uio->uio_iov[i].iov_base;
88 			bp->b_bcount = uio->uio_iov[i].iov_len;
89 			bp->b_offset = uio->uio_offset;
90 			bp->b_saveaddr = sa;
91 
92 			/* Don't exceed drivers iosize limit */
93 			if (bp->b_bcount > dev->si_iosize_max)
94 				bp->b_bcount = dev->si_iosize_max;
95 
96 			/*
97 			 * Make sure the pbuf can map the request
98 			 * XXX: The pbuf has kvasize = MAXPHYS so a request
99 			 * XXX: larger than MAXPHYS - PAGE_SIZE must be
100 			 * XXX: page aligned or it will be fragmented.
101 			 */
102 			iolen = ((vm_offset_t) bp->b_data) & PAGE_MASK;
103 			if ((bp->b_bcount + iolen) > bp->b_kvasize) {
104 				bp->b_bcount = bp->b_kvasize;
105 				if (iolen != 0)
106 					bp->b_bcount -= PAGE_SIZE;
107 			}
108 			bp->b_bufsize = bp->b_bcount;
109 
110 			blockno = bp->b_offset >> DEV_BSHIFT;
111 			if (chk_blockno && (daddr_t)blockno != blockno) {
112 				error = EINVAL; /* blockno overflow */
113 				goto doerror;
114 			}
115 			bp->b_blkno = blockno;
116 
117 			if (uio->uio_segflg == UIO_USERSPACE)
118 				if (vmapbuf(bp) < 0) {
119 					error = EFAULT;
120 					goto doerror;
121 				}
122 
123 			BUF_STRATEGY(bp, 0);
124 			crit_enter();
125 			while ((bp->b_flags & B_DONE) == 0)
126 				tsleep((caddr_t)bp, 0, "physstr", 0);
127 			crit_exit();
128 
129 			if (uio->uio_segflg == UIO_USERSPACE)
130 				vunmapbuf(bp);
131 			iolen = bp->b_bcount - bp->b_resid;
132 			if (iolen == 0 && !(bp->b_flags & B_ERROR))
133 				goto doerror;	/* EOF */
134 			uio->uio_iov[i].iov_len -= iolen;
135 			uio->uio_iov[i].iov_base += iolen;
136 			uio->uio_resid -= iolen;
137 			uio->uio_offset += iolen;
138 			if( bp->b_flags & B_ERROR) {
139 				error = bp->b_error;
140 				goto doerror;
141 			}
142 		}
143 	}
144 doerror:
145 	bp->b_data = sa;
146 	relpbuf(bp, NULL);
147 	return (error);
148 }
149