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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#include <u.h>
#include <libc.h>
#include "cformat.h"
#include "lru.h"
#include "bcache.h"
#include "disk.h"
#include "inode.h"
#include "file.h"
/*
* merge data with that which already exists in a block
*
* we allow only one range per block, always use the new
* data if the ranges don't overlap.
*/
void
fmerge(Dptr *p, char *to, char *from, int start, int len)
{
int end;
end = start + len;
memmove(to+start, from, end-start);
/*
* if ranges do not overlap...
*/
if(start>p->end || p->start>end){
/*
* just use the new data
*/
p->start = start;
p->end = end;
} else {
/*
* merge ranges
*/
if(start < p->start)
p->start = start;
if(end > p->end)
p->end = end;
}
}
/*
* write a block (or less) of data onto a disk, follow it with any necessary
* pointer writes.
*
* N.B. ordering is everything
*/
int
fbwrite(Icache *ic, Ibuf *b, char *a, ulong off, int len)
{
int wrinode;
ulong fbno;
Bbuf *dbb; /* data block */
Bbuf *ibb; /* indirect block */
Dptr *p;
Dptr t;
fbno = off / ic->bsize;
p = &b->inode.ptr;
ibb = 0;
wrinode = 0;
/*
* are there any pages for this inode?
*/
if(p->bno == Notabno){
wrinode = 1;
goto dowrite;
}
/*
* is it an indirect block?
*/
if(p->bno & Indbno){
ibb = bcread(ic, p->bno);
if(ibb == 0)
return -1;
p = (Dptr*)ibb->data;
p += fbno % ic->p2b;
goto dowrite;
}
/*
* is it the wrong direct block?
*/
if((p->fbno%ic->p2b) != (fbno%ic->p2b)){
/*
* yes, make an indirect block
*/
t = *p;
dpalloc(ic, p);
if(p->bno == Notabno){
*p = t;
return -1;
}
ibb = bcalloc(ic, p->bno);
if(ibb == 0){
*p = t;
return -1;
}
p = (Dptr*)ibb->data;
p += t.fbno % ic->p2b;
*p = t;
p = (Dptr*)ibb->data;
p += fbno % ic->p2b;
}
wrinode = 1;
dowrite:
/*
* get the data block into the block cache
*/
if(p->bno == Notabno){
/*
* create a new block
*/
dalloc(ic, p);
if(p->bno == Notabno)
return -1; /* no blocks left (maybe) */
dbb = bcalloc(ic, p->bno);
} else {
/*
* use what's there
*/
dbb = bcread(ic, p->bno);
}
if(dbb == 0)
return -1;
/*
* merge in the new data
*/
if(p->fbno != fbno){
p->start = p->end = 0;
p->fbno = fbno;
}
fmerge(p, dbb->data, a, off % ic->bsize, len);
/*
* write changed blocks back in the
* correct order
*/
bcmark(ic, dbb);
if(ibb)
bcmark(ic, ibb);
if(wrinode)
if(iwrite(ic, b) < 0)
return -1;
return len;
}
/*
* write `n' bytes to the cache
*
* return number of bytes written
*/
long
fwrite(Icache *ic, Ibuf *b, char *a, ulong off, long n)
{
int len;
long sofar;
for(sofar = 0; sofar < n; sofar += len){
len = ic->bsize - ((off+sofar)%ic->bsize);
if(len > n - sofar)
len = n - sofar;
if(fbwrite(ic, b, a+sofar, off+sofar, len) < 0)
return sofar;
}
return sofar;
}
/*
* get a pointer to the next valid data at or after `off'
*/
Dptr *
fpget(Icache *ic, Ibuf *b, ulong off)
{
ulong fbno;
long doff;
Bbuf *ibb; /* indirect block */
Dptr *p, *p0, *pf;
fbno = off / ic->bsize;
p = &b->inode.ptr;
/*
* are there any pages for this inode?
*/
if(p->bno == Notabno)
return 0;
/*
* if it's a direct block, life is easy?
*/
if(!(p->bno & Indbno)){
/*
* a direct block, return p if it's at least past what we want
*/
if(p->fbno > fbno)
return p;
if(p->fbno < fbno)
return 0;
doff = off % ic->bsize;
if(doff>=p->start && doff<p->end)
return p;
else
return 0;
}
/*
* read the indirect block
*/
ibb = bcread(ic, p->bno);
if(ibb == 0)
return 0;
/*
* find the next valid pointer
*/
p0 = (Dptr*)ibb->data;
pf = p0 + (fbno % ic->p2b);
if(pf->bno!=Notabno && pf->fbno==fbno){
doff = off % ic->bsize;
if(doff<pf->end)
return pf;
}
for(p = pf+1; p < p0 + ic->p2b; p++){
fbno++;
if(p->fbno==fbno && p->bno!=Notabno && p->start<p->end)
return p;
}
for(p = p0; p < pf; p++){
fbno++;
if(p->fbno==fbno && p->bno!=Notabno && p->start<p->end)
return p;
}
return 0;
}
/*
* read `n' bytes from the cache.
*
* if we hit a gap and we've read something,
* return number of bytes read so far.
*
* if we start with a gap, return minus the number of bytes
* to the next data.
*
* if there are no bytes cached, return 0.
*/
long
fread(Icache *ic, Ibuf *b, char *a, ulong off, long n)
{
int len, start;
long sofar, gap;
Dptr *p;
Bbuf *bb;
for(sofar = 0; sofar < n; sofar += len, off += len){
/*
* get pointer to next data
*/
len = n - sofar;
p = fpget(ic, b, off);
/*
* if no more data, return what we have so far
*/
if(p == 0)
return sofar;
/*
* if there's a gap, return the size of the gap
*/
gap = (ic->bsize*p->fbno + p->start) - off;
if(gap>0)
if(sofar == 0)
return -gap;
else
return sofar;
/*
* return what we have
*/
bb = bcread(ic, p->bno);
if(bb == 0)
return sofar;
start = p->start - gap;
if(p->end - start < len)
len = p->end - start;
memmove(a + sofar, bb->data + start, len);
}
return sofar;
}
|