Fix a minor bug that prevented compilation.
[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 $
9a71d53f 20 * $DragonFly: src/sys/kern/kern_physio.c,v 1.21 2006/05/03 20:44:49 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
81b5c339 36physwakeup(struct bio *bio)
984263bc 37{
10f3fee5 38 bio->bio_buf->b_cmd = BUF_CMD_DONE;
81b5c339 39 wakeup(bio);
984263bc
MD
40}
41
42int
43physio(dev_t dev, struct uio *uio, int ioflag)
44{
45 int i;
46 int error;
f366cb61 47 int chk_blockno;
a8f169e2 48 int saflags;
3591bbc6
MD
49 int iolen;
50 int bcount;
984263bc
MD
51 struct buf *bp;
52
984263bc 53 bp = getpbuf(NULL);
a8f169e2 54 saflags = bp->b_flags;
984263bc
MD
55 error = 0;
56
57 /* XXX: sanity check */
58 if(dev->si_iosize_max < PAGE_SIZE) {
59 printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
60 devtoname(dev), dev->si_iosize_max);
61 dev->si_iosize_max = DFLTPHYS;
62 }
63
f366cb61 64 /* Don't check block number overflow for D_MEM */
978d73e7 65 if ((dev_dflags(dev) & D_TYPEMASK) == D_MEM)
f366cb61
HP
66 chk_blockno = 0;
67 else
68 chk_blockno = 1;
69
984263bc
MD
70 for (i = 0; i < uio->uio_iovcnt; i++) {
71 while (uio->uio_iov[i].iov_len) {
72 if (uio->uio_rw == UIO_READ)
10f3fee5 73 bp->b_cmd = BUF_CMD_READ;
984263bc 74 else
10f3fee5
MD
75 bp->b_cmd = BUF_CMD_WRITE;
76 bp->b_flags = saflags;
3591bbc6 77 bcount = uio->uio_iov[i].iov_len;
984263bc 78
81b5c339
MD
79 reinitbufbio(bp); /* clear translation cache */
80 bp->b_bio1.bio_offset = uio->uio_offset;
81 bp->b_bio1.bio_done = physwakeup;
82
984263bc 83 /* Don't exceed drivers iosize limit */
3591bbc6
MD
84 if (bcount > dev->si_iosize_max)
85 bcount = dev->si_iosize_max;
984263bc
MD
86
87 /*
88 * Make sure the pbuf can map the request
89 * XXX: The pbuf has kvasize = MAXPHYS so a request
90 * XXX: larger than MAXPHYS - PAGE_SIZE must be
91 * XXX: page aligned or it will be fragmented.
92 */
f2eaa325
MD
93 iolen = ((vm_offset_t) uio->uio_iov[i].iov_base) &
94 PAGE_MASK;
3591bbc6
MD
95 if ((bcount + iolen) > bp->b_kvasize) {
96 bcount = bp->b_kvasize;
984263bc 97 if (iolen != 0)
3591bbc6 98 bcount -= PAGE_SIZE;
984263bc 99 }
81b5c339 100 if (uio->uio_segflg == UIO_USERSPACE) {
3591bbc6 101 if (vmapbuf(bp, uio->uio_iov[i].iov_base, bcount) < 0) {
984263bc
MD
102 error = EFAULT;
103 goto doerror;
104 }
3591bbc6
MD
105 } else {
106 bp->b_data = uio->uio_iov[i].iov_base;
107 bp->b_bcount = bcount;
81b5c339
MD
108 }
109 dev_dstrategy(dev, &bp->b_bio1);
e43a034f 110 crit_enter();
10f3fee5 111 while (bp->b_cmd != BUF_CMD_DONE)
81b5c339 112 tsleep(&bp->b_bio1, 0, "physstr", 0);
e43a034f 113 crit_exit();
984263bc
MD
114
115 if (uio->uio_segflg == UIO_USERSPACE)
116 vunmapbuf(bp);
117 iolen = bp->b_bcount - bp->b_resid;
118 if (iolen == 0 && !(bp->b_flags & B_ERROR))
119 goto doerror; /* EOF */
120 uio->uio_iov[i].iov_len -= iolen;
121 uio->uio_iov[i].iov_base += iolen;
122 uio->uio_resid -= iolen;
123 uio->uio_offset += iolen;
124 if( bp->b_flags & B_ERROR) {
125 error = bp->b_error;
126 goto doerror;
127 }
128 }
129 }
130doerror:
131 relpbuf(bp, NULL);
984263bc
MD
132 return (error);
133}