-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbio.c
258 lines (235 loc) · 5.44 KB
/
bio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "types.h"
#include "param.h"
#include "print.h"
#include "conf.h"
#include "proc.h"
#include "buf.h"
#include "cpu.h"
struct {
struct spinlock lock; /* Spin lock */
struct buf *head; /* First buffer in list */
struct buf *tail; /* Last buffer in list */
} bfreelist;
/*
* Removes a buffer from a queue with the given head and tail pointers..
*/
void rmbuf(struct buf *bp, struct buf **head, struct buf **tail) {
struct buf **bpp;
bpp = head;
while (*bpp && *bpp != bp)
bpp = &(*bpp)->forw;
if (bp && bp != bp->forw) {
bp->back->forw = bp->forw;
bp->forw->back = bp->back;
*bpp = bp->forw;
} else {
*bpp = NULL;
*tail = NULL;
}
}
/*
* Retrieves an empty buffer.
*/
struct buf *geteblk(size_t size) {
struct buf *bp;
loop:
acquire(&bfreelist.lock);
if ((bp = bfreelist.head) == NULL) {
release(&bp->lock);
sleep(&bfreelist);
goto loop;
}
for (bp = bfreelist.head; bp->forw != NULL; bp++) {
acquire(&bp->lock);
if (bp->size == size)
break;
release(&bp->lock);
}
rmbuf(bp, &bfreelist.head, &bfreelist.tail);
release(&bfreelist.lock);
if (bp->flags&B_DIRTY) {
bp->flags |= B_ASYNC;
bwrite(bp);
release(&bp->lock);
goto loop;
}
bp->flags |= B_LOCK;
release(&bp->lock);
return bp;
}
/*
* Pick up the device's error number and pass it to the user. If there is an
* error but the number is 0 set a generalized code. Actually the latter is
* always true because devices don't yet return specific errors.
*/
void geterror(struct buf *bp) {
struct thread *td;
td = mycpu()->thread;
if ((td->error = bp->error) == 0)
td->error = EIO;
}
/*
* Yields until I/O completion on provided buffer. Returns error to the user.
*/
void iowait(struct buf *bp) {
while ((bp->flags&B_DONE) == 0)
sleep(bp);
geterror(bp);
}
/*
* Determines if there is a buffer associated with the given block already in
* memory.
*/
struct buf *incore(int dev, size_t blkno) {
struct devtab *dp;
struct buf *bp;
dp = blkdevs[major(dev)].tab;
for (bp = dp->head; bp != NULL; bp = bp->forw) {
acquire(&bp->lock);
if (bp->dev == dev && bp->blkno == blkno) {
release(&bp->lock);
return bp;
}
release(&bp->lock);
}
return NULL;
}
/*
* Allocates buffer and reads from device into buffer (or yields for buffer to
* be available).
*/
static struct buf *getblk(int dev, size_t blkno) {
struct devtab *dp;
struct buf *bp;
size_t size;
if (major(dev) < NBLKDEV)
panic("blkdev");
size = blkdevs[major(dev)].blksz;
loop:
if ((dp = blkdevs[major(dev)].tab) == NULL)
panic("devtab");
for (bp = dp->head; bp != NULL; bp = bp->forw) {
acquire(&bp->lock);
if (bp->blkno != blkno || bp->dev != dev || bp->size != size)
goto loop;
if (bp->flags & B_LOCK) {
release(&bp->lock);
sleep(bp);
goto loop;
}
bp->flags |= B_LOCK;
release(&bp->lock);
return bp;
}
bp = geteblk(size);
acquire(&bp->lock);
bp->dev = dev;
bp->blkno = blkno;
bp->size = size;
release(&bp->lock);
return bp;
}
/*
* Releases the given buffer.
*/
void brelse(struct buf *bp) {
struct devtab *dp;
acquire(&bp->lock);
if (bp->flags & B_WANTED)
wakeup(bp);
acquire(&bfreelist.lock);
wakeup(&bfreelist);
if (bp->flags & B_ERROR)
bp->dev = NODEV;
bp->flags &= ~(B_WANTED|B_BUSY|B_ASYNC);
if ((dp = &blkdevs[major(bp->dev)]) == NULL)
panic("devtab");
acquire(&dp->lock);
rmbuf(bp, &dp->head, &dp->tail);
rmbuf(bp, &dp->iohead, &dp->iotail);
release(&dp.lock);
if (bfreelist.tail == NULL)
bfreelist.tail = bp;
bp->forw = bfreelist.head;
bp->back = NULL;
bfreelist.head = bp;
release(&bp->lock);
release(&bfreelist.lock);
}
/*
* Reads reads a block from a block device, allocates a buffer to store the
* data, and returns the allocated buffer.
*
* If no buffer is found, then yields for a buffer to be available.
*/
struct buf *bread(int dev, size_t blkno) {
struct buf *bp;
bp = getblk(dev, blkno);
if (bp->flags&B_DONE)
return bp;
bp->flags |= B_READ;
blkdevs[major(dev)].strat(bp);
iowait(bp);
return bp;
}
/*
* Reads a block from a block device like bread(), but also starts I/O on the
* given read-ahead block (which it automatically allocates).
*/
struct buf *breada(int dev, size_t blkno, size_t rablkno) {
struct buf *bp, *rabp;
size_t blksz;
blksz = blkdevs[major(dev)].blksz;
if (incore(dev, blkno)) {
bp = getblk(dev, blkno);
if ((bp->flags & B_DONE) == 0) {
bp->flags |= B_READ;
blkdevs[major(dev)].strat(bp);
}
}
if (rablkno && !incore(dev, rablkno)) {
rabp = getblk(dev, rablkno);
if ((rabp->flags & B_DONE) == 0)
brelse(rabp);
else {
rabp->flags |= B_READ | B_ASYNC;
blkdevs[major(dev)].strat(rabp);
}
}
if (bp == NULL)
return bread(dev, blkno);
iowait(bp);
return bp;
}
/*
* Writes the buffer to its device and releases the buffer.
*/
void bwrite(struct buf *bp) {
int flags;
flags = bp->flags;
bp->flags &= ~(B_WRITE | B_DONE | B_ERROR | B_DIRTY);
blkdevs[minor(bp->dev)].strat(bp);
if ((flags&B_ASYNC) == 0) {
iowait(bp);
brelse(bp);
} else if ((flags&B_DIRTY) == 0)
geterror(bp);
}
/*
* Initialize all buffers. The only thing that needs to be done is moving them
* all to the freelist. No locks are needed since this runs on one CPU only,
* but are still used for good measure.
*/
void binit(void) {
struct buf *bp;
acquire(&bfreelist.lock);
for (bp = &bufs[0]; bp < &bufs[NBUF]; bp++) {
acquire(&bp->lock);
bp->forw = bfreelist.head;
if (bfreelist.tail == NULL)
bfreelist.tail = bp;
bfreelist.head = bp;
release(&bp->lock);
}
release(&bfreelist.lock);
}