2 056fe1ba 2003-11-23 devnull * Memory-only VtBlock cache.
4 056fe1ba 2003-11-23 devnull * The cached Venti blocks are in the hash chains.
5 056fe1ba 2003-11-23 devnull * The cached local blocks are only in the blocks array.
6 056fe1ba 2003-11-23 devnull * The free blocks are in the heap, which is supposed to
7 056fe1ba 2003-11-23 devnull * be indexed by second-to-last use but actually
8 056fe1ba 2003-11-23 devnull * appears to be last use.
11 056fe1ba 2003-11-23 devnull #include <u.h>
12 056fe1ba 2003-11-23 devnull #include <libc.h>
13 056fe1ba 2003-11-23 devnull #include <venti.h>
15 056fe1ba 2003-11-23 devnull int nread, ncopy, nwrite;
18 056fe1ba 2003-11-23 devnull BioLocal = 1,
19 056fe1ba 2003-11-23 devnull BioVenti,
20 056fe1ba 2003-11-23 devnull BioReading,
21 056fe1ba 2003-11-23 devnull BioWriting,
22 056fe1ba 2003-11-23 devnull BioEmpty,
23 056fe1ba 2003-11-23 devnull BioVentiError,
26 056fe1ba 2003-11-23 devnull BadHeap = ~0,
28 056fe1ba 2003-11-23 devnull struct VtCache
30 056fe1ba 2003-11-23 devnull QLock lk;
31 056fe1ba 2003-11-23 devnull VtConn *z;
32 056fe1ba 2003-11-23 devnull u32int blocksize;
33 056fe1ba 2003-11-23 devnull u32int now; /* ticks for usage time stamps */
34 056fe1ba 2003-11-23 devnull VtBlock **hash; /* hash table for finding addresses */
35 056fe1ba 2003-11-23 devnull int nhash;
36 056fe1ba 2003-11-23 devnull VtBlock **heap; /* heap for finding victims */
37 056fe1ba 2003-11-23 devnull int nheap;
38 056fe1ba 2003-11-23 devnull VtBlock *block; /* all allocated blocks */
39 056fe1ba 2003-11-23 devnull int nblock;
40 056fe1ba 2003-11-23 devnull uchar *mem; /* memory for all blocks and data */
41 a1882dc1 2004-06-17 devnull int (*write)(VtConn*, uchar[VtScoreSize], uint, uchar*, int);
44 056fe1ba 2003-11-23 devnull static void cachecheck(VtCache*);
47 e87f5d7c 2005-07-13 devnull vtcachealloc(VtConn *z, int blocksize, ulong nblock)
49 056fe1ba 2003-11-23 devnull uchar *p;
50 056fe1ba 2003-11-23 devnull VtCache *c;
52 056fe1ba 2003-11-23 devnull VtBlock *b;
54 056fe1ba 2003-11-23 devnull c = vtmallocz(sizeof(VtCache));
56 056fe1ba 2003-11-23 devnull c->z = z;
57 056fe1ba 2003-11-23 devnull c->blocksize = (blocksize + 127) & ~127;
58 056fe1ba 2003-11-23 devnull c->nblock = nblock;
59 056fe1ba 2003-11-23 devnull c->nhash = nblock;
60 056fe1ba 2003-11-23 devnull c->hash = vtmallocz(nblock*sizeof(VtBlock*));
61 056fe1ba 2003-11-23 devnull c->heap = vtmallocz(nblock*sizeof(VtBlock*));
62 056fe1ba 2003-11-23 devnull c->block = vtmallocz(nblock*sizeof(VtBlock));
63 056fe1ba 2003-11-23 devnull c->mem = vtmallocz(nblock*c->blocksize);
64 a1882dc1 2004-06-17 devnull c->write = vtwrite;
66 056fe1ba 2003-11-23 devnull p = c->mem;
67 056fe1ba 2003-11-23 devnull for(i=0; i<nblock; i++){
68 056fe1ba 2003-11-23 devnull b = &c->block[i];
69 056fe1ba 2003-11-23 devnull b->addr = NilBlock;
70 056fe1ba 2003-11-23 devnull b->c = c;
71 056fe1ba 2003-11-23 devnull b->data = p;
72 056fe1ba 2003-11-23 devnull b->heap = i;
73 056fe1ba 2003-11-23 devnull c->heap[i] = b;
74 056fe1ba 2003-11-23 devnull p += c->blocksize;
76 056fe1ba 2003-11-23 devnull c->nheap = nblock;
77 056fe1ba 2003-11-23 devnull cachecheck(c);
78 056fe1ba 2003-11-23 devnull return c;
82 a1882dc1 2004-06-17 devnull * BUG This is here so that vbackup can override it and do some
83 a1882dc1 2004-06-17 devnull * pipelining of writes. Arguably vtwrite or vtwritepacket or the
84 a1882dc1 2004-06-17 devnull * cache itself should be providing this functionality.
87 a1882dc1 2004-06-17 devnull vtcachesetwrite(VtCache *c, int (*write)(VtConn*, uchar[VtScoreSize], uint, uchar*, int))
89 a1882dc1 2004-06-17 devnull if(write == nil)
90 a1882dc1 2004-06-17 devnull write = vtwrite;
91 a1882dc1 2004-06-17 devnull c->write = write;
95 056fe1ba 2003-11-23 devnull vtcachefree(VtCache *c)
99 056fe1ba 2003-11-23 devnull qlock(&c->lk);
101 056fe1ba 2003-11-23 devnull cachecheck(c);
102 056fe1ba 2003-11-23 devnull for(i=0; i<c->nblock; i++)
103 056fe1ba 2003-11-23 devnull assert(c->block[i].ref == 0);
105 056fe1ba 2003-11-23 devnull vtfree(c->hash);
106 056fe1ba 2003-11-23 devnull vtfree(c->heap);
107 056fe1ba 2003-11-23 devnull vtfree(c->block);
108 056fe1ba 2003-11-23 devnull vtfree(c->mem);
109 056fe1ba 2003-11-23 devnull vtfree(c);
112 056fe1ba 2003-11-23 devnull static void
113 056fe1ba 2003-11-23 devnull vtcachedump(VtCache *c)
116 056fe1ba 2003-11-23 devnull VtBlock *b;
118 056fe1ba 2003-11-23 devnull for(i=0; i<c->nblock; i++){
119 056fe1ba 2003-11-23 devnull b = &c->block[i];
120 056fe1ba 2003-11-23 devnull print("cache block %d: type %d score %V iostate %d addr %d ref %d nlock %d\n",
121 056fe1ba 2003-11-23 devnull i, b->type, b->score, b->iostate, b->addr, b->ref, b->nlock);
125 056fe1ba 2003-11-23 devnull static void
126 056fe1ba 2003-11-23 devnull cachecheck(VtCache *c)
128 056fe1ba 2003-11-23 devnull u32int size, now;
129 056fe1ba 2003-11-23 devnull int i, k, refed;
130 056fe1ba 2003-11-23 devnull VtBlock *b;
132 056fe1ba 2003-11-23 devnull size = c->blocksize;
133 056fe1ba 2003-11-23 devnull now = c->now;
135 056fe1ba 2003-11-23 devnull for(i = 0; i < c->nheap; i++){
136 056fe1ba 2003-11-23 devnull if(c->heap[i]->heap != i)
137 056fe1ba 2003-11-23 devnull sysfatal("mis-heaped at %d: %d", i, c->heap[i]->heap);
138 056fe1ba 2003-11-23 devnull if(i > 0 && c->heap[(i - 1) >> 1]->used - now > c->heap[i]->used - now)
139 056fe1ba 2003-11-23 devnull sysfatal("bad heap ordering");
140 056fe1ba 2003-11-23 devnull k = (i << 1) + 1;
141 056fe1ba 2003-11-23 devnull if(k < c->nheap && c->heap[i]->used - now > c->heap[k]->used - now)
142 056fe1ba 2003-11-23 devnull sysfatal("bad heap ordering");
144 056fe1ba 2003-11-23 devnull if(k < c->nheap && c->heap[i]->used - now > c->heap[k]->used - now)
145 056fe1ba 2003-11-23 devnull sysfatal("bad heap ordering");
148 056fe1ba 2003-11-23 devnull refed = 0;
149 056fe1ba 2003-11-23 devnull for(i = 0; i < c->nblock; i++){
150 056fe1ba 2003-11-23 devnull b = &c->block[i];
151 056fe1ba 2003-11-23 devnull if(b->data != &c->mem[i * size])
152 056fe1ba 2003-11-23 devnull sysfatal("mis-blocked at %d", i);
153 056fe1ba 2003-11-23 devnull if(b->ref && b->heap == BadHeap)
154 056fe1ba 2003-11-23 devnull refed++;
155 056fe1ba 2003-11-23 devnull else if(b->addr != NilBlock)
156 056fe1ba 2003-11-23 devnull refed++;
158 056fe1ba 2003-11-23 devnull if(c->nheap + refed != c->nblock){
159 056fe1ba 2003-11-23 devnull fprint(2, "cachecheck: nheap %d refed %d nblocks %d\n", c->nheap, refed, c->nblock);
160 056fe1ba 2003-11-23 devnull //vtcachedump(c);
162 056fe1ba 2003-11-23 devnull assert(c->nheap + refed == c->nblock);
163 056fe1ba 2003-11-23 devnull refed = 0;
164 056fe1ba 2003-11-23 devnull for(i = 0; i < c->nblock; i++){
165 056fe1ba 2003-11-23 devnull b = &c->block[i];
166 056fe1ba 2003-11-23 devnull if(b->ref){
167 056fe1ba 2003-11-23 devnull if(1)fprint(2, "a=%ud %V ref=%d\n", b->addr, b->score, b->ref);
168 056fe1ba 2003-11-23 devnull refed++;
171 056fe1ba 2003-11-23 devnull if(refed > 0)fprint(2, "cachecheck: in used %d\n", refed);
174 056fe1ba 2003-11-23 devnull static int
175 056fe1ba 2003-11-23 devnull upheap(int i, VtBlock *b)
177 056fe1ba 2003-11-23 devnull VtBlock *bb;
178 056fe1ba 2003-11-23 devnull u32int now;
180 056fe1ba 2003-11-23 devnull VtCache *c;
182 056fe1ba 2003-11-23 devnull c = b->c;
183 056fe1ba 2003-11-23 devnull now = c->now;
184 056fe1ba 2003-11-23 devnull for(; i != 0; i = p){
185 056fe1ba 2003-11-23 devnull p = (i - 1) >> 1;
186 056fe1ba 2003-11-23 devnull bb = c->heap[p];
187 056fe1ba 2003-11-23 devnull if(b->used - now >= bb->used - now)
189 056fe1ba 2003-11-23 devnull c->heap[i] = bb;
190 056fe1ba 2003-11-23 devnull bb->heap = i;
192 056fe1ba 2003-11-23 devnull c->heap[i] = b;
193 056fe1ba 2003-11-23 devnull b->heap = i;
195 056fe1ba 2003-11-23 devnull return i;
198 056fe1ba 2003-11-23 devnull static int
199 056fe1ba 2003-11-23 devnull downheap(int i, VtBlock *b)
201 056fe1ba 2003-11-23 devnull VtBlock *bb;
202 056fe1ba 2003-11-23 devnull u32int now;
204 056fe1ba 2003-11-23 devnull VtCache *c;
206 056fe1ba 2003-11-23 devnull c = b->c;
207 056fe1ba 2003-11-23 devnull now = c->now;
208 056fe1ba 2003-11-23 devnull for(; ; i = k){
209 056fe1ba 2003-11-23 devnull k = (i << 1) + 1;
210 056fe1ba 2003-11-23 devnull if(k >= c->nheap)
212 056fe1ba 2003-11-23 devnull if(k + 1 < c->nheap && c->heap[k]->used - now > c->heap[k + 1]->used - now)
214 056fe1ba 2003-11-23 devnull bb = c->heap[k];
215 056fe1ba 2003-11-23 devnull if(b->used - now <= bb->used - now)
217 056fe1ba 2003-11-23 devnull c->heap[i] = bb;
218 056fe1ba 2003-11-23 devnull bb->heap = i;
220 056fe1ba 2003-11-23 devnull c->heap[i] = b;
221 056fe1ba 2003-11-23 devnull b->heap = i;
222 056fe1ba 2003-11-23 devnull return i;
226 056fe1ba 2003-11-23 devnull * Delete a block from the heap.
227 056fe1ba 2003-11-23 devnull * Called with c->lk held.
229 056fe1ba 2003-11-23 devnull static void
230 056fe1ba 2003-11-23 devnull heapdel(VtBlock *b)
232 056fe1ba 2003-11-23 devnull int i, si;
233 056fe1ba 2003-11-23 devnull VtCache *c;
235 056fe1ba 2003-11-23 devnull c = b->c;
237 056fe1ba 2003-11-23 devnull si = b->heap;
238 056fe1ba 2003-11-23 devnull if(si == BadHeap)
240 056fe1ba 2003-11-23 devnull b->heap = BadHeap;
241 056fe1ba 2003-11-23 devnull c->nheap--;
242 056fe1ba 2003-11-23 devnull if(si == c->nheap)
244 056fe1ba 2003-11-23 devnull b = c->heap[c->nheap];
245 056fe1ba 2003-11-23 devnull i = upheap(si, b);
246 056fe1ba 2003-11-23 devnull if(i == si)
247 056fe1ba 2003-11-23 devnull downheap(i, b);
251 056fe1ba 2003-11-23 devnull * Insert a block into the heap.
252 056fe1ba 2003-11-23 devnull * Called with c->lk held.
254 056fe1ba 2003-11-23 devnull static void
255 056fe1ba 2003-11-23 devnull heapins(VtBlock *b)
257 056fe1ba 2003-11-23 devnull assert(b->heap == BadHeap);
258 056fe1ba 2003-11-23 devnull upheap(b->c->nheap++, b);
262 056fe1ba 2003-11-23 devnull * locate the vtBlock with the oldest second to last use.
263 056fe1ba 2003-11-23 devnull * remove it from the heap, and fix up the heap.
265 056fe1ba 2003-11-23 devnull /* called with c->lk held */
266 056fe1ba 2003-11-23 devnull static VtBlock*
267 056fe1ba 2003-11-23 devnull vtcachebumpblock(VtCache *c)
269 056fe1ba 2003-11-23 devnull VtBlock *b;
272 056fe1ba 2003-11-23 devnull * locate the vtBlock with the oldest second to last use.
273 056fe1ba 2003-11-23 devnull * remove it from the heap, and fix up the heap.
275 056fe1ba 2003-11-23 devnull if(c->nheap == 0){
276 056fe1ba 2003-11-23 devnull vtcachedump(c);
277 7cb74894 2004-06-17 devnull fprint(2, "vtcachebumpblock: no free blocks in vtCache");
278 7cb74894 2004-06-17 devnull abort();
280 056fe1ba 2003-11-23 devnull b = c->heap[0];
281 056fe1ba 2003-11-23 devnull heapdel(b);
283 056fe1ba 2003-11-23 devnull assert(b->heap == BadHeap);
284 056fe1ba 2003-11-23 devnull assert(b->ref == 0);
287 056fe1ba 2003-11-23 devnull * unchain the vtBlock from hash chain if any
289 056fe1ba 2003-11-23 devnull if(b->prev){
290 056fe1ba 2003-11-23 devnull *(b->prev) = b->next;
291 056fe1ba 2003-11-23 devnull if(b->next)
292 056fe1ba 2003-11-23 devnull b->next->prev = b->prev;
293 056fe1ba 2003-11-23 devnull b->prev = nil;
297 056fe1ba 2003-11-23 devnull if(0)fprint(2, "droping %x:%V\n", b->addr, b->score);
298 056fe1ba 2003-11-23 devnull /* set vtBlock to a reasonable state */
299 056fe1ba 2003-11-23 devnull b->ref = 1;
300 056fe1ba 2003-11-23 devnull b->iostate = BioEmpty;
301 056fe1ba 2003-11-23 devnull return b;
305 056fe1ba 2003-11-23 devnull * fetch a local block from the memory cache.
306 056fe1ba 2003-11-23 devnull * if it's not there, load it, bumping some other Block.
307 056fe1ba 2003-11-23 devnull * if we're out of free blocks, we're screwed.
309 056fe1ba 2003-11-23 devnull VtBlock*
310 056fe1ba 2003-11-23 devnull vtcachelocal(VtCache *c, u32int addr, int type)
312 056fe1ba 2003-11-23 devnull VtBlock *b;
314 056fe1ba 2003-11-23 devnull if(addr >= c->nblock)
315 056fe1ba 2003-11-23 devnull sysfatal("vtcachelocal: asked for block #%ud; only %d blocks\n",
316 056fe1ba 2003-11-23 devnull addr, c->nblock);
318 056fe1ba 2003-11-23 devnull b = &c->block[addr];
319 056fe1ba 2003-11-23 devnull if(b->addr == NilBlock || b->iostate != BioLocal)
320 056fe1ba 2003-11-23 devnull sysfatal("vtcachelocal: block is not local");
322 056fe1ba 2003-11-23 devnull if(b->type != type)
323 056fe1ba 2003-11-23 devnull sysfatal("vtcachelocal: block has wrong type %d != %d", b->type, type);
325 056fe1ba 2003-11-23 devnull qlock(&c->lk);
326 056fe1ba 2003-11-23 devnull b->ref++;
327 056fe1ba 2003-11-23 devnull qunlock(&c->lk);
329 056fe1ba 2003-11-23 devnull qlock(&b->lk);
330 056fe1ba 2003-11-23 devnull b->nlock = 1;
331 056fe1ba 2003-11-23 devnull return b;
334 056fe1ba 2003-11-23 devnull VtBlock*
335 056fe1ba 2003-11-23 devnull vtcacheallocblock(VtCache *c, int type)
337 056fe1ba 2003-11-23 devnull VtBlock *b;
339 056fe1ba 2003-11-23 devnull qlock(&c->lk);
340 056fe1ba 2003-11-23 devnull b = vtcachebumpblock(c);
341 056fe1ba 2003-11-23 devnull b->iostate = BioLocal;
342 056fe1ba 2003-11-23 devnull b->type = type;
343 056fe1ba 2003-11-23 devnull b->addr = b - c->block;
344 056fe1ba 2003-11-23 devnull vtzeroextend(type, b->data, 0, c->blocksize);
345 056fe1ba 2003-11-23 devnull vtlocaltoglobal(b->addr, b->score);
346 056fe1ba 2003-11-23 devnull qunlock(&c->lk);
348 056fe1ba 2003-11-23 devnull qlock(&b->lk);
349 056fe1ba 2003-11-23 devnull b->nlock = 1;
351 056fe1ba 2003-11-23 devnull return b;
355 056fe1ba 2003-11-23 devnull * fetch a global (Venti) block from the memory cache.
356 056fe1ba 2003-11-23 devnull * if it's not there, load it, bumping some other block.
358 056fe1ba 2003-11-23 devnull VtBlock*
359 056fe1ba 2003-11-23 devnull vtcacheglobal(VtCache *c, uchar score[VtScoreSize], int type)
361 056fe1ba 2003-11-23 devnull VtBlock *b;
362 056fe1ba 2003-11-23 devnull ulong h;
364 056fe1ba 2003-11-23 devnull u32int addr;
366 056fe1ba 2003-11-23 devnull addr = vtglobaltolocal(score);
367 056fe1ba 2003-11-23 devnull if(addr != NilBlock)
368 056fe1ba 2003-11-23 devnull return vtcachelocal(c, addr, type);
370 056fe1ba 2003-11-23 devnull h = (u32int)(score[0]|(score[1]<<8)|(score[2]<<16)|(score[3]<<24)) % c->nhash;
373 056fe1ba 2003-11-23 devnull * look for the block in the cache
375 056fe1ba 2003-11-23 devnull qlock(&c->lk);
376 056fe1ba 2003-11-23 devnull for(b = c->hash[h]; b != nil; b = b->next){
377 056fe1ba 2003-11-23 devnull if(b->addr != NilBlock || memcmp(b->score, score, VtScoreSize) != 0 || b->type != type)
378 056fe1ba 2003-11-23 devnull continue;
379 056fe1ba 2003-11-23 devnull heapdel(b);
380 056fe1ba 2003-11-23 devnull b->ref++;
381 056fe1ba 2003-11-23 devnull qunlock(&c->lk);
382 056fe1ba 2003-11-23 devnull qlock(&b->lk);
383 056fe1ba 2003-11-23 devnull b->nlock = 1;
384 0c148046 2004-06-16 devnull if(b->iostate == BioVentiError){
385 eacc220a 2004-06-17 devnull if(chattyventi)
386 eacc220a 2004-06-17 devnull fprint(2, "cached read error for %V\n", score);
387 0c148046 2004-06-16 devnull werrstr("venti i/o error");
388 0c148046 2004-06-16 devnull vtblockput(b);
389 0c148046 2004-06-16 devnull return nil;
391 056fe1ba 2003-11-23 devnull return b;
395 056fe1ba 2003-11-23 devnull * not found
397 056fe1ba 2003-11-23 devnull b = vtcachebumpblock(c);
398 056fe1ba 2003-11-23 devnull b->addr = NilBlock;
399 056fe1ba 2003-11-23 devnull b->type = type;
400 056fe1ba 2003-11-23 devnull memmove(b->score, score, VtScoreSize);
401 056fe1ba 2003-11-23 devnull /* chain onto correct hash */
402 056fe1ba 2003-11-23 devnull b->next = c->hash[h];
403 056fe1ba 2003-11-23 devnull c->hash[h] = b;
404 056fe1ba 2003-11-23 devnull if(b->next != nil)
405 056fe1ba 2003-11-23 devnull b->next->prev = &b->next;
406 056fe1ba 2003-11-23 devnull b->prev = &c->hash[h];
409 056fe1ba 2003-11-23 devnull * Lock b before unlocking c, so that others wait while we read.
411 056fe1ba 2003-11-23 devnull * You might think there is a race between this qlock(b) before qunlock(c)
412 056fe1ba 2003-11-23 devnull * and the qlock(c) while holding a qlock(b) in vtblockwrite. However,
413 056fe1ba 2003-11-23 devnull * the block here can never be the block in a vtblockwrite, so we're safe.
414 056fe1ba 2003-11-23 devnull * We're certainly living on the edge.
416 056fe1ba 2003-11-23 devnull qlock(&b->lk);
417 056fe1ba 2003-11-23 devnull b->nlock = 1;
418 056fe1ba 2003-11-23 devnull qunlock(&c->lk);
420 056fe1ba 2003-11-23 devnull n = vtread(c->z, score, type, b->data, c->blocksize);
421 056fe1ba 2003-11-23 devnull if(n < 0){
422 a1882dc1 2004-06-17 devnull werrstr("vtread %V: %r", score);
423 eacc220a 2004-06-17 devnull if(chattyventi)
424 eacc220a 2004-06-17 devnull fprint(2, "read %V: %r\n", score);
425 056fe1ba 2003-11-23 devnull b->iostate = BioVentiError;
426 056fe1ba 2003-11-23 devnull vtblockput(b);
427 056fe1ba 2003-11-23 devnull return nil;
429 056fe1ba 2003-11-23 devnull vtzeroextend(type, b->data, n, c->blocksize);
430 056fe1ba 2003-11-23 devnull b->iostate = BioVenti;
431 056fe1ba 2003-11-23 devnull b->nlock = 1;
432 056fe1ba 2003-11-23 devnull return b;
436 056fe1ba 2003-11-23 devnull * The thread that has locked b may refer to it by
437 056fe1ba 2003-11-23 devnull * multiple names. Nlock counts the number of
438 056fe1ba 2003-11-23 devnull * references the locking thread holds. It will call
439 056fe1ba 2003-11-23 devnull * vtblockput once per reference.
442 056fe1ba 2003-11-23 devnull vtblockduplock(VtBlock *b)
444 056fe1ba 2003-11-23 devnull assert(b->nlock > 0);
445 056fe1ba 2003-11-23 devnull b->nlock++;
449 056fe1ba 2003-11-23 devnull * we're done with the block.
450 056fe1ba 2003-11-23 devnull * unlock it. can't use it after calling this.
453 056fe1ba 2003-11-23 devnull vtblockput(VtBlock* b)
455 056fe1ba 2003-11-23 devnull VtCache *c;
457 056fe1ba 2003-11-23 devnull if(b == nil)
460 056fe1ba 2003-11-23 devnull if(0)fprint(2, "vtblockput: %d: %x %d %d\n", getpid(), b->addr, c->nheap, b->iostate);
462 056fe1ba 2003-11-23 devnull if(--b->nlock > 0)
466 056fe1ba 2003-11-23 devnull * b->nlock should probably stay at zero while
467 056fe1ba 2003-11-23 devnull * the vtBlock is unlocked, but diskThread and vtSleep
468 056fe1ba 2003-11-23 devnull * conspire to assume that they can just qlock(&b->lk); vtblockput(b),
469 056fe1ba 2003-11-23 devnull * so we have to keep b->nlock set to 1 even
470 056fe1ba 2003-11-23 devnull * when the vtBlock is unlocked.
472 056fe1ba 2003-11-23 devnull assert(b->nlock == 0);
473 056fe1ba 2003-11-23 devnull b->nlock = 1;
475 056fe1ba 2003-11-23 devnull qunlock(&b->lk);
476 056fe1ba 2003-11-23 devnull c = b->c;
477 056fe1ba 2003-11-23 devnull qlock(&c->lk);
479 056fe1ba 2003-11-23 devnull if(--b->ref > 0){
480 056fe1ba 2003-11-23 devnull qunlock(&c->lk);
484 056fe1ba 2003-11-23 devnull assert(b->ref == 0);
485 056fe1ba 2003-11-23 devnull switch(b->iostate){
486 056fe1ba 2003-11-23 devnull case BioVenti:
487 056fe1ba 2003-11-23 devnull //if(b->addr != NilBlock) print("blockput %d\n", b->addr);
488 056fe1ba 2003-11-23 devnull b->used = c->now++;
489 056fe1ba 2003-11-23 devnull case BioVentiError:
490 056fe1ba 2003-11-23 devnull heapins(b);
492 056fe1ba 2003-11-23 devnull case BioLocal:
495 056fe1ba 2003-11-23 devnull qunlock(&c->lk);
499 056fe1ba 2003-11-23 devnull vtblockwrite(VtBlock *b)
501 056fe1ba 2003-11-23 devnull uchar score[VtScoreSize];
502 056fe1ba 2003-11-23 devnull VtCache *c;
506 056fe1ba 2003-11-23 devnull if(b->iostate != BioLocal){
507 0c148046 2004-06-16 devnull werrstr("vtblockwrite: not a local block");
508 0c148046 2004-06-16 devnull return -1;
511 056fe1ba 2003-11-23 devnull c = b->c;
512 056fe1ba 2003-11-23 devnull n = vtzerotruncate(b->type, b->data, c->blocksize);
513 a1882dc1 2004-06-17 devnull if(c->write(c->z, score, b->type, b->data, n) < 0)
514 056fe1ba 2003-11-23 devnull return -1;
516 056fe1ba 2003-11-23 devnull memmove(b->score, score, VtScoreSize);
518 056fe1ba 2003-11-23 devnull qlock(&c->lk);
519 056fe1ba 2003-11-23 devnull b->iostate = BioVenti;
520 056fe1ba 2003-11-23 devnull h = (u32int)(score[0]|(score[1]<<8)|(score[2]<<16)|(score[3]<<24)) % c->nhash;
521 056fe1ba 2003-11-23 devnull b->next = c->hash[h];
522 056fe1ba 2003-11-23 devnull c->hash[h] = b;
523 056fe1ba 2003-11-23 devnull if(b->next != nil)
524 056fe1ba 2003-11-23 devnull b->next->prev = &b->next;
525 056fe1ba 2003-11-23 devnull b->prev = &c->hash[h];
526 056fe1ba 2003-11-23 devnull qunlock(&c->lk);
527 056fe1ba 2003-11-23 devnull return 0;
531 056fe1ba 2003-11-23 devnull vtcacheblocksize(VtCache *c)
533 056fe1ba 2003-11-23 devnull return c->blocksize;
536 056fe1ba 2003-11-23 devnull VtBlock*
537 056fe1ba 2003-11-23 devnull vtblockcopy(VtBlock *b)
539 056fe1ba 2003-11-23 devnull VtBlock *bb;
541 056fe1ba 2003-11-23 devnull ncopy++;
542 056fe1ba 2003-11-23 devnull bb = vtcacheallocblock(b->c, b->type);
543 056fe1ba 2003-11-23 devnull if(bb == nil){
544 056fe1ba 2003-11-23 devnull vtblockput(b);
545 056fe1ba 2003-11-23 devnull return nil;
547 056fe1ba 2003-11-23 devnull memmove(bb->data, b->data, b->c->blocksize);
548 056fe1ba 2003-11-23 devnull vtblockput(b);
549 056fe1ba 2003-11-23 devnull return bb;
553 056fe1ba 2003-11-23 devnull vtlocaltoglobal(u32int addr, uchar score[VtScoreSize])
555 056fe1ba 2003-11-23 devnull memset(score, 0, 16);
556 056fe1ba 2003-11-23 devnull score[16] = addr>>24;
557 056fe1ba 2003-11-23 devnull score[17] = addr>>16;
558 056fe1ba 2003-11-23 devnull score[18] = addr>>8;
559 056fe1ba 2003-11-23 devnull score[19] = addr;
564 056fe1ba 2003-11-23 devnull vtglobaltolocal(uchar score[VtScoreSize])
566 056fe1ba 2003-11-23 devnull static uchar zero[16];
567 056fe1ba 2003-11-23 devnull if(memcmp(score, zero, 16) != 0)
568 056fe1ba 2003-11-23 devnull return NilBlock;
569 056fe1ba 2003-11-23 devnull return (score[16]<<24)|(score[17]<<16)|(score[18]<<8)|score[19];