Properly assert the state of the MP lock in the async syscall message
[dragonfly.git] / sys / kern / kern_physio.c
CommitLineData
984263bc
MD
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 *
f366cb61 19 * $FreeBSD: src/sys/kern/kern_physio.c,v 1.46.2.4 2003/11/14 09:51:47 simokawa Exp $
e38910da 20 * $DragonFly: src/sys/kern/kern_physio.c,v 1.13 2005/11/19 17:58:21 dillon Exp $
984263bc
MD
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>
335dda38 29#include <sys/device.h>
54048233 30#include <sys/thread2.h>
984263bc
MD
31
32#include <vm/vm.h>
33#include <vm/vm_extern.h>
34
35static void
36physwakeup(struct buf *bp)
37{
38 wakeup((caddr_t) bp);
39}
40
41int
42physio(dev_t dev, struct uio *uio, int ioflag)
43{
44 int i;
45 int error;
f366cb61 46 int chk_blockno;
984263bc
MD
47 caddr_t sa;
48 off_t blockno;
49 u_int iolen;
50 struct buf *bp;
51
984263bc
MD
52 bp = getpbuf(NULL);
53 sa = bp->b_data;
54 error = 0;
55
56 /* XXX: sanity check */
57 if(dev->si_iosize_max < PAGE_SIZE) {
58 printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
59 devtoname(dev), dev->si_iosize_max);
60 dev->si_iosize_max = DFLTPHYS;
61 }
62
f366cb61 63 /* Don't check block number overflow for D_MEM */
978d73e7 64 if ((dev_dflags(dev) & D_TYPEMASK) == D_MEM)
f366cb61
HP
65 chk_blockno = 0;
66 else
67 chk_blockno = 1;
68
984263bc
MD
69 for (i = 0; i < uio->uio_iovcnt; i++) {
70 while (uio->uio_iov[i].iov_len) {
71 if (uio->uio_rw == UIO_READ)
5ff4cfb5 72 bp->b_flags = B_PHYS | B_READ;
984263bc 73 else
5ff4cfb5 74 bp->b_flags = B_PHYS | B_WRITE;
984263bc
MD
75 bp->b_dev = dev;
76 bp->b_iodone = physwakeup;
77 bp->b_data = uio->uio_iov[i].iov_base;
78 bp->b_bcount = uio->uio_iov[i].iov_len;
79 bp->b_offset = uio->uio_offset;
80 bp->b_saveaddr = sa;
81
82 /* Don't exceed drivers iosize limit */
83 if (bp->b_bcount > dev->si_iosize_max)
84 bp->b_bcount = dev->si_iosize_max;
85
86 /*
87 * Make sure the pbuf can map the request
88 * XXX: The pbuf has kvasize = MAXPHYS so a request
89 * XXX: larger than MAXPHYS - PAGE_SIZE must be
90 * XXX: page aligned or it will be fragmented.
91 */
92 iolen = ((vm_offset_t) bp->b_data) & PAGE_MASK;
93 if ((bp->b_bcount + iolen) > bp->b_kvasize) {
94 bp->b_bcount = bp->b_kvasize;
95 if (iolen != 0)
96 bp->b_bcount -= PAGE_SIZE;
97 }
98 bp->b_bufsize = bp->b_bcount;
99
100 blockno = bp->b_offset >> DEV_BSHIFT;
f366cb61 101 if (chk_blockno && (daddr_t)blockno != blockno) {
984263bc
MD
102 error = EINVAL; /* blockno overflow */
103 goto doerror;
104 }
105 bp->b_blkno = blockno;
106
107 if (uio->uio_segflg == UIO_USERSPACE)
108 if (vmapbuf(bp) < 0) {
109 error = EFAULT;
110 goto doerror;
111 }
112
113 BUF_STRATEGY(bp, 0);
e43a034f 114 crit_enter();
984263bc 115 while ((bp->b_flags & B_DONE) == 0)
377d4740 116 tsleep((caddr_t)bp, 0, "physstr", 0);
e43a034f 117 crit_exit();
984263bc
MD
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_flags & B_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_flags & B_ERROR) {
129 error = bp->b_error;
130 goto doerror;
131 }
132 }
133 }
134doerror:
d094eee8 135 bp->b_data = sa;
984263bc 136 relpbuf(bp, NULL);
984263bc
MD
137 return (error);
138}