Split the lwkt_token code out of lwkt_thread.c. Give it its own file.
[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 *
19 * $FreeBSD: src/sys/kern/kern_physio.c,v 1.46.2.3 2003/05/29 06:15:35 alc Exp $
335dda38 20 * $DragonFly: src/sys/kern/kern_physio.c,v 1.5 2003/07/22 17:03:33 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>
984263bc
MD
30
31#include <vm/vm.h>
32#include <vm/vm_extern.h>
33
34static void
35physwakeup(struct buf *bp)
36{
37 wakeup((caddr_t) bp);
38}
39
40int
41physio(dev_t dev, struct uio *uio, int ioflag)
42{
43 int i;
44 int error;
45 int spl;
46 caddr_t sa;
47 off_t blockno;
48 u_int iolen;
49 struct buf *bp;
50
05edc21a
MD
51 /*
52 * NOTE: we no longer have to PHOLD() the process, because the
53 * kernel stack / uarea cannot be swapped, and when it can be in
54 * the future it will only happen if the process is sleeping on
55 * a particular address.
56 */
984263bc
MD
57 bp = getpbuf(NULL);
58 sa = bp->b_data;
59 error = 0;
60
61 /* XXX: sanity check */
62 if(dev->si_iosize_max < PAGE_SIZE) {
63 printf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
64 devtoname(dev), dev->si_iosize_max);
65 dev->si_iosize_max = DFLTPHYS;
66 }
67
68 for (i = 0; i < uio->uio_iovcnt; i++) {
69 while (uio->uio_iov[i].iov_len) {
70 if (uio->uio_rw == UIO_READ)
71 bp->b_flags = B_PHYS | B_CALL | B_READ;
72 else
73 bp->b_flags = B_PHYS | B_CALL | B_WRITE;
74 bp->b_dev = dev;
75 bp->b_iodone = physwakeup;
76 bp->b_data = uio->uio_iov[i].iov_base;
77 bp->b_bcount = uio->uio_iov[i].iov_len;
78 bp->b_offset = uio->uio_offset;
79 bp->b_saveaddr = sa;
80
81 /* Don't exceed drivers iosize limit */
82 if (bp->b_bcount > dev->si_iosize_max)
83 bp->b_bcount = dev->si_iosize_max;
84
85 /*
86 * Make sure the pbuf can map the request
87 * XXX: The pbuf has kvasize = MAXPHYS so a request
88 * XXX: larger than MAXPHYS - PAGE_SIZE must be
89 * XXX: page aligned or it will be fragmented.
90 */
91 iolen = ((vm_offset_t) bp->b_data) & PAGE_MASK;
92 if ((bp->b_bcount + iolen) > bp->b_kvasize) {
93 bp->b_bcount = bp->b_kvasize;
94 if (iolen != 0)
95 bp->b_bcount -= PAGE_SIZE;
96 }
97 bp->b_bufsize = bp->b_bcount;
98
99 blockno = bp->b_offset >> DEV_BSHIFT;
100 if ((daddr_t)blockno != blockno) {
101 error = EINVAL; /* blockno overflow */
102 goto doerror;
103 }
104 bp->b_blkno = blockno;
105
106 if (uio->uio_segflg == UIO_USERSPACE)
107 if (vmapbuf(bp) < 0) {
108 error = EFAULT;
109 goto doerror;
110 }
111
112 BUF_STRATEGY(bp, 0);
113 spl = splbio();
114 while ((bp->b_flags & B_DONE) == 0)
377d4740 115 tsleep((caddr_t)bp, 0, "physstr", 0);
984263bc
MD
116 splx(spl);
117
118 if (uio->uio_segflg == UIO_USERSPACE)
119 vunmapbuf(bp);
120 iolen = bp->b_bcount - bp->b_resid;
121 if (iolen == 0 && !(bp->b_flags & B_ERROR))
122 goto doerror; /* EOF */
123 uio->uio_iov[i].iov_len -= iolen;
124 uio->uio_iov[i].iov_base += iolen;
125 uio->uio_resid -= iolen;
126 uio->uio_offset += iolen;
127 if( bp->b_flags & B_ERROR) {
128 error = bp->b_error;
129 goto doerror;
130 }
131 }
132 }
133doerror:
134 relpbuf(bp, NULL);
984263bc
MD
135 return (error);
136}