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