2 * Manage tree of VtFiles stored in the block cache.
4 * The single point of truth for the info about the VtFiles themselves
5 * is the block data. Because of this, there is no explicit locking of
6 * VtFile structures, and indeed there may be more than one VtFile
7 * structure for a given Venti file. They synchronize through the
10 * This is a bit simpler than fossil because there are no epochs
11 * or tags or anything else. Just mutable local blocks and immutable
24 static char EBadEntry[] = "bad VtEntry";
25 static char ENotDir[] = "walk in non-directory";
26 static char ETooBig[] = "file too big";
27 static char EBadAddr[] = "bad address";
28 static char ELabelMismatch[] = "label mismatch";
30 static int sizetodepth(uvlong s, int psize, int dsize);
31 static VtBlock *fileload(VtFile *r, VtEntry *e);
32 static int shrinkdepth(VtFile*, VtBlock*, VtEntry*, int);
33 static int shrinksize(VtFile*, VtEntry*, uvlong);
34 static int growdepth(VtFile*, VtBlock*, VtEntry*, int);
36 #define ISLOCKED(r) ((r)->b != nil)
37 #define DEPTH(t) ((t)&VtTypeDepthMask)
40 vtfilealloc(VtCache *c, VtBlock *b, VtFile *p, u32int offset, int mode)
47 assert(p==nil || ISLOCKED(p));
53 epb = p->dsize / VtEntrySize;
55 if(b->type != VtDirType)
59 * a non-active entry is the only thing that
60 * can legitimately happen here. all the others
63 if(vtentryunpack(&e, b->data, offset % epb) < 0){
64 fprint(2, "vtentryunpack failed\n");
67 if(!(e.flags & VtEntryActive)){
68 if(0)fprint(2, "not active\n");
71 if(e.psize < 256 || e.dsize < 256){
72 fprint(2, "psize %ud dsize %ud\n", e.psize, e.dsize);
76 if(DEPTH(e.type) < sizetodepth(e.size, e.psize, e.dsize)){
77 fprint(2, "depth %ud size %llud psize %ud dsize %ud\n",
78 DEPTH(e.type), e.size, e.psize, e.dsize);
82 size = vtcacheblocksize(c);
83 if(e.dsize > size || e.psize > size){
84 fprint(2, "psize %ud dsize %ud blocksize %ud\n", e.psize, e.dsize, size);
88 r = vtmallocz(sizeof(VtFile));
93 r->dir = (e.type & VtTypeBaseMask) == VtDirType;
98 assert(mode == VtOREAD || p->mode == VtORDWR);
102 memmove(r->score, b->score, VtScoreSize);
114 vtfileroot(VtCache *c, u32int addr, int mode)
119 b = vtcachelocal(c, addr, VtDirType);
123 r = vtfilealloc(c, b, nil, 0, mode);
129 vtfileopenroot(VtCache *c, VtEntry *e)
134 b = vtcacheallocblock(c, VtDirType);
138 vtentrypack(e, b->data, 0);
139 f = vtfilealloc(c, b, nil, 0, VtORDWR);
145 vtfilecreateroot(VtCache *c, int psize, int dsize, int type)
149 memset(&e, 0, sizeof e);
150 e.flags = VtEntryActive;
154 memmove(e.score, vtzeroscore, VtScoreSize);
156 return vtfileopenroot(c, &e);
160 vtfileopen(VtFile *r, u32int offset, int mode)
171 bn = offset/(r->dsize/VtEntrySize);
173 b = vtfileblock(r, bn, mode);
176 r = vtfilealloc(r->c, b, r, offset, mode);
182 vtfilecreate(VtFile *r, int psize, int dsize, int dir)
193 assert(psize <= VtMaxLumpSize);
194 assert(dsize <= VtMaxLumpSize);
201 epb = r->dsize/VtEntrySize;
203 size = vtfilegetdirsize(r);
205 * look at a random block to see if we can find an empty entry
207 offset = lnrand(size+1);
208 offset -= offset % epb;
210 /* try the given block and then try the last block */
213 b = vtfileblock(r, bn, VtORDWR);
216 for(i=offset%r->epb; i<epb; i++){
217 if(vtentryunpack(&e, b->data, i) < 0)
219 if((e.flags&VtEntryActive) == 0 && e.gen != ~0)
224 fprint(2, "vtfilecreate: cannot happen\n");
225 werrstr("vtfilecreate: cannot happen");
232 /* found an entry - gen already set */
235 e.flags = VtEntryActive;
236 e.type = dir ? VtDirType : VtDataType;
238 memmove(e.score, vtzeroscore, VtScoreSize);
239 vtentrypack(&e, b->data, i);
243 if(vtfilesetdirsize(r, offset+1) < 0){
249 rr = vtfilealloc(r->c, b, r, offset, VtORDWR);
255 vtfilekill(VtFile *r, int doremove)
265 if(doremove==0 && e.size == 0){
266 /* already truncated */
278 e.flags &= ~VtEntryLocal;
281 memmove(e.score, vtzeroscore, VtScoreSize);
282 vtentrypack(&e, b->data, r->offset % r->epb);
294 vtfileremove(VtFile *r)
296 return vtfilekill(r, 1);
300 vtfiletruncate(VtFile *r)
302 return vtfilekill(r, 0);
306 vtfilegetsize(VtFile *r)
321 shrinksize(VtFile *r, VtEntry *e, uvlong size)
323 int i, depth, type, isdir, ppb;
325 uchar score[VtScoreSize];
328 b = vtcacheglobal(r->c, e->score, e->type);
333 ppb = e->psize/VtScoreSize;
336 for(i=0; i+1<depth; i++)
341 if(b->addr == NilBlock){
342 /* not worth copying the block just so we can zero some of it */
348 * invariant: each pointer in the tree rooted at b accounts for ptrsz bytes
351 /* zero the pointers to unnecessary blocks */
352 i = (size+ptrsz-1)/ptrsz;
354 memmove(b->data+i*VtScoreSize, vtzeroscore, VtScoreSize);
356 /* recurse (go around again) on the partially necessary block */
365 memmove(score, b->data+i*VtScoreSize, VtScoreSize);
367 b = vtcacheglobal(r->c, score, type);
372 if(b->addr == NilBlock){
378 * No one ever truncates BtDir blocks.
380 if(depth==0 && !isdir && e->dsize > size)
381 memset(b->data+size, 0, e->dsize-size);
387 vtfilesetsize(VtFile *r, uvlong size)
395 return vtfiletruncate(r);
397 if(size > VtMaxFileSize || size > ((uvlong)MaxBlock)*r->dsize){
412 depth = sizetodepth(size, e.psize, e.dsize);
413 edepth = DEPTH(e.type);
415 if(shrinkdepth(r, b, &e, depth) < 0){
419 }else if(depth > edepth){
420 if(growdepth(r, b, &e, depth) < 0){
427 shrinksize(r, &e, size);
430 vtentrypack(&e, b->data, r->offset % r->epb);
437 vtfilesetdirsize(VtFile *r, u32int ds)
443 epb = r->dsize/VtEntrySize;
445 size = (uvlong)r->dsize*(ds/epb);
446 size += VtEntrySize*(ds%epb);
447 return vtfilesetsize(r, size);
451 vtfilegetdirsize(VtFile *r)
458 epb = r->dsize/VtEntrySize;
460 size = vtfilegetsize(r);
461 ds = epb*(size/r->dsize);
462 ds += (size%r->dsize)/VtEntrySize;
467 vtfilegetentry(VtFile *r, VtEntry *e)
481 vtfilesetentry(VtFile *r, VtEntry *e)
487 b = fileload(r, &ee);
490 vtentrypack(e, b->data, r->offset % r->epb);
496 blockwalk(VtBlock *p, int index, VtCache *c, int mode, VtEntry *e)
512 score = p->data+index*VtScoreSize;
515 //print("walk from %V/%d ty %d to %V ty %d\n", p->score, index, p->type, score, type);
517 if(mode == VtOWRITE && vtglobaltolocal(score) == NilBlock){
518 b = vtcacheallocblock(c, type);
522 b = vtcacheglobal(c, score, type);
524 if(b == nil || mode == VtOREAD)
527 if(vtglobaltolocal(b->score) != NilBlock)
535 e->flags |= VtEntryLocal;
537 b = vtblockcopy(b/*, e->tag, fs->ehi, fs->elo*/);
542 if(p->type == VtDirType){
543 memmove(e->score, b->score, VtScoreSize);
544 vtentrypack(e, p->data, index);
546 memmove(p->data+index*VtScoreSize, b->score, VtScoreSize);
552 * Change the depth of the VtFile r.
553 * The entry e for r is contained in block p.
556 growdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth)
562 assert(depth <= VtPointerDepth);
564 b = vtcacheglobal(r->c, e->score, e->type);
571 * Keep adding layers until we get to the right depth
572 * or an error occurs.
574 while(DEPTH(e->type) < depth){
575 bb = vtcacheallocblock(r->c, e->type+1);
578 memmove(bb->data, b->score, VtScoreSize);
579 memmove(e->score, bb->score, VtScoreSize);
581 e->flags |= VtEntryLocal;
586 vtentrypack(e, p->data, r->offset % r->epb);
589 if(DEPTH(e->type) == depth)
595 shrinkdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth)
597 VtBlock *b, *nb, *ob, *rb;
601 assert(depth <= VtPointerDepth);
603 rb = vtcacheglobal(r->c, e->score, e->type);
608 * Walk down to the new root block.
609 * We may stop early, but something is better than nothing.
615 for(; DEPTH(e->type) > depth; e->type--){
616 nb = vtcacheglobal(r->c, b->data, e->type-1);
619 if(ob!=nil && ob!=rb)
631 * Right now, e points at the root block rb, b is the new root block,
632 * and ob points at b. To update:
634 * (i) change e to point at b
635 * (ii) zero the pointer ob -> b
636 * (iii) free the root block
638 * p (the block containing e) must be written before
643 memmove(e->score, b->score, VtScoreSize);
644 vtentrypack(e, p->data, r->offset % r->epb);
647 memmove(ob->data, vtzeroscore, VtScoreSize);
651 if(ob!=nil && ob!=rb)
655 if(DEPTH(e->type) == depth)
661 mkindices(VtEntry *e, u32int bn, int *index)
665 memset(index, 0, VtPointerDepth*sizeof(int));
667 np = e->psize/VtScoreSize;
668 for(i=0; bn > 0; i++){
669 if(i >= VtPointerDepth){
670 werrstr("bad address 0x%lux", (ulong)bn);
680 vtfileblock(VtFile *r, u32int bn, int mode)
683 int index[VtPointerDepth+1];
689 assert(bn != NilBlock);
695 i = mkindices(&e, bn, index);
698 if(i > DEPTH(e.type)){
700 werrstr("bad address 0x%lux", (ulong)bn);
704 if(growdepth(r, b, &e, i) < 0)
708 assert(b->type == VtDirType);
710 index[DEPTH(e.type)] = r->offset % r->epb;
712 /* mode for intermediate block */
717 for(i=DEPTH(e.type); i>=0; i--){
718 bb = blockwalk(b, index[i], r->c, i==0 ? mode : m, &e);
731 vtfileblockscore(VtFile *r, u32int bn, uchar score[VtScoreSize])
734 int index[VtPointerDepth+1];
739 assert(bn != NilBlock);
745 i = mkindices(&e, bn, index);
750 if(i > DEPTH(e.type)){
751 memmove(score, vtzeroscore, VtScoreSize);
756 index[DEPTH(e.type)] = r->offset % r->epb;
758 for(i=DEPTH(e.type); i>=1; i--){
759 bb = blockwalk(b, index[i], r->c, VtOREAD, &e);
764 if(memcmp(b->score, vtzeroscore, VtScoreSize) == 0)
768 memmove(score, b->data+index[0]*VtScoreSize, VtScoreSize);
778 vtfileincref(VtFile *r)
786 vtfileclose(VtFile *r)
799 vtfileclose(r->parent);
800 memset(r, ~0, sizeof(*r));
805 * Retrieve the block containing the entry for r.
806 * If a snapshot has happened, we might need
807 * to get a new copy of the block. We avoid this
808 * in the common case by caching the score for
809 * the block and the last epoch in which it was valid.
811 * We use r->mode to tell the difference between active
812 * file system VtFiles (VtORDWR) and VtFiles for the
813 * snapshot file system (VtOREAD).
816 fileloadblock(VtFile *r, int mode)
826 assert(r->mode == VtORDWR);
828 b = vtcacheglobal(r->c, r->score, VtDirType);
833 assert(r->parent != nil);
834 if(vtfilelock(r->parent, VtORDWR) < 0)
836 b = vtfileblock(r->parent, r->offset/r->epb, VtORDWR);
837 vtfileunlock(r->parent);
840 memmove(r->score, b->score, VtScoreSize);
846 werrstr("read/write lock of read-only file");
849 addr = vtglobaltolocal(r->score);
851 return vtcacheglobal(r->c, r->score, VtDirType);
853 b = vtcachelocal(r->c, addr, VtDirType);
858 * If it failed because the epochs don't match, the block has been
859 * archived and reclaimed. Rewalk from the parent and get the
860 * new pointer. This can't happen in the VtORDWR case
861 * above because blocks in the current epoch don't get
862 * reclaimed. The fact that we're VtOREAD means we're
863 * a snapshot. (Or else the file system is read-only, but then
864 * the archiver isn't going around deleting blocks.)
866 rerrstr(e, sizeof e);
867 if(strcmp(e, ELabelMismatch) == 0){
868 if(vtfilelock(r->parent, VtOREAD) < 0)
870 b = vtfileblock(r->parent, r->offset/r->epb, VtOREAD);
871 vtfileunlock(r->parent);
873 fprint(2, "vtfilealloc: lost %V found %V\n",
875 memmove(r->score, b->score, VtScoreSize);
884 vtfilelock(VtFile *r, int mode)
891 b = fileloadblock(r, mode);
895 * The fact that we are holding b serves as the
896 * lock entitling us to write to r->b.
904 * Lock two (usually sibling) VtFiles. This needs special care
905 * because the Entries for both vtFiles might be in the same block.
906 * We also try to lock blocks in left-to-right order within the tree.
909 vtfilelock2(VtFile *r, VtFile *rr, int mode)
914 return vtfilelock(r, mode);
919 if(r->parent==rr->parent && r->offset/r->epb == rr->offset/rr->epb){
920 b = fileloadblock(r, mode);
925 }else if(r->parent==rr->parent || r->offset > rr->offset){
926 bb = fileloadblock(rr, mode);
927 b = fileloadblock(r, mode);
929 b = fileloadblock(r, mode);
930 bb = fileloadblock(rr, mode);
932 if(b == nil || bb == nil){
941 * The fact that we are holding b and bb serves
942 * as the lock entitling us to write to r->b and rr->b.
950 vtfileunlock(VtFile *r)
955 fprint(2, "vtfileunlock: already unlocked\n");
964 fileload(VtFile *r, VtEntry *e)
970 if(vtentryunpack(e, b->data, r->offset % r->epb) < 0)
977 sizetodepth(uvlong s, int psize, int dsize)
982 /* determine pointer depth */
983 np = psize/VtScoreSize;
984 s = (s + dsize - 1)/dsize;
985 for(d = 0; s > 1; d++)
991 vtfileread(VtFile *f, void *data, long count, vlong offset)
999 vtfilegetentry(f, &e);
1002 if(count < 0 || offset < 0){
1003 werrstr("vtfileread: bad offset or count");
1006 if(offset >= e.size)
1009 if(offset+count > e.size)
1010 count = e.size - offset;
1012 frag = offset % e.dsize;
1013 if(frag+count > e.dsize)
1014 count = e.dsize - frag;
1016 b = vtfileblock(f, offset/e.dsize, VtOREAD);
1020 memmove(data, b->data+frag, count);
1026 filewrite1(VtFile *f, void *data, long count, vlong offset)
1032 vtfilegetentry(f, &e);
1033 if(count < 0 || offset < 0){
1034 werrstr("vtfilewrite: bad offset or count");
1038 frag = offset % e.dsize;
1039 if(frag+count > e.dsize)
1040 count = e.dsize - frag;
1043 if(frag == 0 && count == e.dsize)
1046 b = vtfileblock(f, offset/e.dsize, m);
1050 memmove(b->data+frag, data, count);
1052 if(offset+count > e.size){
1053 vtfilegetentry(f, &e);
1054 e.size = offset+count;
1055 vtfilesetentry(f, &e);
1063 vtfilewrite(VtFile *f, void *data, long count, vlong offset)
1067 assert(ISLOCKED(f));
1072 m = filewrite1(f, (char*)data+tot, count-tot, offset+tot);
1083 flushblock(VtCache *c, VtBlock *bb, uchar score[VtScoreSize], int ppb, int epb,
1091 addr = vtglobaltolocal(score);
1092 if(addr == NilBlock)
1097 if(memcmp(b->score, score, VtScoreSize) != 0)
1100 if((b = vtcachelocal(c, addr, type)) == nil)
1108 for(i=0; i<epb; i++){
1109 if(vtentryunpack(&e, b->data, i) < 0)
1111 if(flushblock(c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize,
1117 default: /* VtPointerTypeX */
1118 for(i=0; i<ppb; i++){
1119 if(flushblock(c, nil, b->data+VtScoreSize*i, ppb, epb, type-1) < 0)
1125 if(vtblockwrite(b) < 0)
1127 memmove(score, b->score, VtScoreSize);
1139 vtfileflush(VtFile *f)
1145 assert(ISLOCKED(f));
1146 b = fileload(f, &e);
1147 if(!(e.flags&VtEntryLocal)){
1152 ret = flushblock(f->c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize,
1159 vtentrypack(&e, b->data, f->offset % f->epb);
1165 vtfileflushbefore(VtFile *r, u64int offset)
1169 int i, base, depth, ppb, epb, ok;
1170 int index[VtPointerDepth+1], index1[VtPointerDepth+1], j, ret;
1171 VtBlock *bi[VtPointerDepth+2];
1174 assert(ISLOCKED(r));
1178 b = fileload(r, &e);
1183 memset(bi, 0, sizeof bi);
1184 depth = DEPTH(e.type);
1186 i = mkindices(&e, (offset-1)/e.dsize, index);
1191 mkindices(&e, offset/e.dsize, index1);
1192 ppb = e.psize / VtScoreSize;
1193 epb = e.dsize / VtEntrySize;
1195 index[depth] = r->offset % r->epb;
1196 for(i=depth; i>=0; i--){
1197 bb = blockwalk(b, index[i], r->c, VtORDWR, &e);
1205 base = e.type&~VtTypeDepthMask;
1206 for(i=0; i<depth; i++){
1208 /* bottom: data or dir block */
1209 ok = offset%e.dsize == 0;
1211 /* middle: pointer blocks */
1214 * flush everything up to the break
1216 for(j=0; j<index[i-1]; j++)
1217 if(flushblock(r->c, nil, b->data+j*VtScoreSize, ppb, epb, base+i-1) < 0)
1220 * if the rest of the block is already flushed,
1221 * we can flush the whole block.
1225 if(vtglobaltolocal(b->data+j*VtScoreSize) != NilBlock)
1232 score = bi[i+1]->data+index[i]*VtScoreSize;
1233 if(flushblock(r->c, bi[i], score, ppb, epb, base+i) < 0)
1239 /* top: entry. do this always so that the score is up-to-date */
1240 vtentrypack(&e, bi[depth+1]->data, index[depth]);
1241 for(i=0; i<nelem(bi); i++)