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