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
|
#include <u.h>
#include <libc.h>
#include <diskfs.h>
#include <venti.h>
extern void vtlibthread(void);
typedef struct DiskVenti DiskVenti;
struct DiskVenti
{
Disk disk;
VtEntry e;
VtCache *c;
};
extern int nfilereads;
/*
* This part is like file.c but doesn't require storing the root block
* in the cache permanently and doesn't care about locking since
* all the blocks are read-only. Perhaps at some point this functionality
* should go into libvac in some form.
*/
static int
vtfileindices(VtEntry *e, u32int bn, int *index)
{
int i, np;
memset(index, 0, VtPointerDepth*sizeof(int));
np = e->psize/VtScoreSize;
memset(index, 0, sizeof(index));
for(i=0; bn > 0; i++){
if(i >= VtPointerDepth){
werrstr("bad block number %lud", (ulong)bn);
return -1;
}
index[i] = bn % np;
bn /= np;
}
return i;
}
static VtBlock*
_vtfileblock(VtCache *c, VtEntry *e, u32int bn)
{
VtBlock *b, *bb;
int i, d, index[VtPointerDepth+1], t;
i = vtfileindices(e, bn, index);
if(i < 0)
return nil;
d = (e->type&VtTypeDepthMask);
if(i > d){
werrstr("bad address %d > %d (%x %x)", i, d, e->type, e->flags);
return nil;
}
//fprint(2, "vtread %V\n", e->score);
b = vtcacheglobal(c, e->score, e->type);
if(b == nil)
return nil;
for(i=d-1; i>=0; i--){
t = VtDataType+i;
//fprint(2, "vtread %V\n", b->data+index[i]*VtScoreSize);
bb = vtcacheglobal(c, b->data+index[i]*VtScoreSize, t);
vtblockput(b);
if(bb == nil)
return nil;
b = bb;
}
return b;
}
static void
diskventiblockput(Block *b)
{
vtblockput(b->priv);
free(b);
}
static Block*
diskventiread(Disk *dd, u32int len, u64int offset)
{
DiskVenti *d = (DiskVenti*)dd;
VtBlock *vb;
Block *b;
int frag;
nfilereads++;
vb = _vtfileblock(d->c, &d->e, offset/d->e.dsize);
if(vb == nil)
return nil;
b = mallocz(sizeof(Block), 1);
if(b == nil){
vtblockput(vb);
return nil;
}
b->priv = vb;
b->_close = diskventiblockput;
frag = offset%d->e.dsize;
b->data = (uchar*)vb->data + frag;
b->len = d->e.dsize - frag;
if(b->len > len)
b->len = len;
return b;
}
static void
diskventiclose(Disk *dd)
{
DiskVenti *d = (DiskVenti*)dd;
free(d);
}
Disk*
diskopenventi(VtCache *c, uchar score[VtScoreSize])
{
DiskVenti *d;
VtEntry e;
VtRoot root;
VtBlock *b;
if((b = vtcacheglobal(c, score, VtRootType)) == nil)
goto Err;
if(vtrootunpack(&root, b->data) < 0)
goto Err;
if(root.blocksize < 512 || (root.blocksize&(root.blocksize-1))){
werrstr("bad blocksize %d", root.blocksize);
goto Err;
}
vtblockput(b);
if((b = vtcacheglobal(c, root.score, VtDirType)) == nil)
goto Err;
if(vtentryunpack(&e, b->data, 0) < 0)
goto Err;
vtblockput(b);
b = nil;
if((e.type&VtTypeBaseMask) != VtDataType){
werrstr("not a single file");
goto Err;
}
d = mallocz(sizeof(DiskVenti), 1);
if(d == nil)
goto Err;
d->disk._read = diskventiread;
d->disk._close = diskventiclose;
d->e = e;
d->c = c;
return &d->disk;
Err:
if(b)
vtblockput(b);
return nil;
}
|