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
19 #define MaxBlock (1UL<<31)
21 static char ENotDir[] = "walk in non-directory";
22 static char ETooBig[] = "file too big";
23 /* static char EBadAddr[] = "bad address"; */
24 static char ELabelMismatch[] = "label mismatch";
26 static int sizetodepth(uvlong s, int psize, int dsize);
27 static VtBlock *fileload(VtFile *r, VtEntry *e);
28 static int shrinkdepth(VtFile*, VtBlock*, VtEntry*, int);
29 static int shrinksize(VtFile*, VtEntry*, uvlong);
30 static int growdepth(VtFile*, VtBlock*, VtEntry*, int);
32 #define ISLOCKED(r) ((r)->b != nil)
33 #define DEPTH(t) ((t)&VtTypeDepthMask)
36 vtfilealloc(VtCache *c, VtBlock *b, VtFile *p, u32int offset, int mode)
42 assert(p==nil || ISLOCKED(p));
48 epb = p->dsize / VtEntrySize;
50 if(b->type != VtDirType){
51 werrstr("bad block type %#uo", b->type);
56 * a non-active entry is the only thing that
57 * can legitimately happen here. all the others
60 if(vtentryunpack(&e, b->data, offset % epb) < 0){
61 fprint(2, "vtentryunpack failed: %r (%.*H)\n", VtEntrySize, b->data+VtEntrySize*(offset%epb));
64 if(!(e.flags & VtEntryActive)){
65 werrstr("entry not active");
69 if(DEPTH(e.type) < sizetodepth(e.size, e.psize, e.dsize)){
70 fprint(2, "depth %ud size %llud psize %ud dsize %ud\n",
71 DEPTH(e.type), e.size, e.psize, e.dsize);
76 r = vtmallocz(sizeof(VtFile));
83 r->dir = (e.type & VtTypeBaseMask) == VtDirType;
88 assert(mode == VtOREAD || p->mode == VtORDWR);
92 assert(b->addr != NilBlock);
95 memmove(r->score, b->score, VtScoreSize);
103 vtfileroot(VtCache *c, u32int addr, int mode)
108 b = vtcachelocal(c, addr, VtDirType);
111 r = vtfilealloc(c, b, nil, 0, mode);
117 vtfileopenroot(VtCache *c, VtEntry *e)
122 b = vtcacheallocblock(c, VtDirType, VtEntrySize);
126 vtentrypack(e, b->data, 0);
127 f = vtfilealloc(c, b, nil, 0, VtORDWR);
133 vtfilecreateroot(VtCache *c, int psize, int dsize, int type)
137 memset(&e, 0, sizeof e);
138 e.flags = VtEntryActive;
142 memmove(e.score, vtzeroscore, VtScoreSize);
144 return vtfileopenroot(c, &e);
148 vtfileopen(VtFile *r, u32int offset, int mode)
159 bn = offset/(r->dsize/VtEntrySize);
161 b = vtfileblock(r, bn, mode);
164 r = vtfilealloc(r->c, b, r, offset, mode);
170 vtfilecreate(VtFile *r, int psize, int dsize, int type)
172 return _vtfilecreate(r, -1, psize, dsize, type);
176 _vtfilecreate(VtFile *r, int o, int psize, int dsize, int type)
187 assert(type == VtDirType || type == VtDataType);
194 epb = r->dsize/VtEntrySize;
196 size = vtfilegetdirsize(r);
198 * look at a random block to see if we can find an empty entry
201 offset = lnrand(size+1);
202 offset -= offset % epb;
206 /* try the given block and then try the last block */
209 b = vtfileblock(r, bn, VtORDWR);
212 for(i=offset%r->epb; i<epb; i++){
213 if(vtentryunpack(&e, b->data, i) < 0)
215 if((e.flags&VtEntryActive) == 0 && e.gen != ~0)
220 fprint(2, "vtfilecreate: cannot happen\n");
221 werrstr("vtfilecreate: cannot happen");
228 /* found an entry - gen already set */
231 e.flags = VtEntryActive;
234 memmove(e.score, vtzeroscore, VtScoreSize);
235 vtentrypack(&e, b->data, i);
239 if(vtfilesetdirsize(r, offset+1) < 0){
245 rr = vtfilealloc(r->c, b, r, offset, VtORDWR);
251 vtfilekill(VtFile *r, int doremove)
261 if(doremove==0 && e.size == 0){
262 /* already truncated */
274 e.flags &= ~VtEntryLocal;
277 memmove(e.score, vtzeroscore, VtScoreSize);
278 vtentrypack(&e, b->data, r->offset % r->epb);
290 vtfileremove(VtFile *r)
292 return vtfilekill(r, 1);
296 vtfiletruncate(VtFile *r)
298 return vtfilekill(r, 0);
302 vtfilegetsize(VtFile *r)
317 shrinksize(VtFile *r, VtEntry *e, uvlong size)
319 int i, depth, bsiz, type, isdir, ppb;
321 uchar score[VtScoreSize];
324 b = vtcacheglobal(r->c, e->score, e->type, r->dsize);
329 ppb = e->psize/VtScoreSize;
332 for(i=0; i+1<depth; i++)
336 while(DEPTH(type) > 0){
337 if(b->addr == NilBlock){
338 /* not worth copying the block just so we can zero some of it */
344 * invariant: each pointer in the tree rooted at b accounts for ptrsz bytes
347 /* zero the pointers to unnecessary blocks */
348 i = (size+ptrsz-1)/ptrsz;
350 memmove(b->data+i*VtScoreSize, vtzeroscore, VtScoreSize);
352 /* recurse (go around again) on the partially necessary block */
361 memmove(score, b->data+i*VtScoreSize, VtScoreSize);
363 if(type == VtDataType || type == VtDirType)
367 b = vtcacheglobal(r->c, score, type, bsiz);
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, u64int 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(VtFile *r, 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(type == VtDirType || type == VtDataType)
521 if(mode == VtOWRITE && vtglobaltolocal(score) == NilBlock){
522 b = vtcacheallocblock(c, type, size);
526 b = vtcacheglobal(c, score, type, size);
528 if(b == nil || mode == VtOREAD)
531 if(vtglobaltolocal(b->score) != NilBlock)
539 e->flags |= VtEntryLocal;
541 b = vtblockcopy(b/*, e->tag, fs->ehi, fs->elo*/);
546 if(p->type == VtDirType){
547 memmove(e->score, b->score, VtScoreSize);
548 vtentrypack(e, p->data, index);
550 memmove(p->data+index*VtScoreSize, b->score, VtScoreSize);
556 * Change the depth of the VtFile r.
557 * The entry e for r is contained in block p.
560 growdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth)
566 assert(depth <= VtPointerDepth);
568 b = vtcacheglobal(r->c, e->score, e->type, r->dsize);
575 * Keep adding layers until we get to the right depth
576 * or an error occurs.
578 while(DEPTH(e->type) < depth){
579 bb = vtcacheallocblock(r->c, e->type+1, r->psize);
582 memmove(bb->data, b->score, VtScoreSize);
583 memmove(e->score, bb->score, VtScoreSize);
585 e->flags |= VtEntryLocal;
590 vtentrypack(e, p->data, r->offset % r->epb);
593 if(DEPTH(e->type) == depth)
599 shrinkdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth)
601 VtBlock *b, *nb, *ob, *rb;
605 assert(depth <= VtPointerDepth);
607 rb = vtcacheglobal(r->c, e->score, e->type, r->psize);
612 * Walk down to the new root block.
613 * We may stop early, but something is better than nothing.
619 for(; DEPTH(e->type) > depth; e->type--){
620 nb = vtcacheglobal(r->c, b->data, e->type-1, r->psize);
623 if(ob!=nil && ob!=rb)
635 * Right now, e points at the root block rb, b is the new root block,
636 * and ob points at b. To update:
638 * (i) change e to point at b
639 * (ii) zero the pointer ob -> b
640 * (iii) free the root block
642 * p (the block containing e) must be written before
647 memmove(e->score, b->score, VtScoreSize);
648 vtentrypack(e, p->data, r->offset % r->epb);
651 memmove(ob->data, vtzeroscore, VtScoreSize);
655 if(ob!=nil && ob!=rb)
659 if(DEPTH(e->type) == depth)
665 mkindices(VtEntry *e, u32int bn, int *index)
669 memset(index, 0, (VtPointerDepth+1)*sizeof(int));
671 np = e->psize/VtScoreSize;
672 for(i=0; bn > 0; i++){
673 if(i >= VtPointerDepth){
674 werrstr("bad address 0x%lux", (ulong)bn);
684 vtfileblock(VtFile *r, u32int bn, int mode)
687 int index[VtPointerDepth+1];
693 assert(bn != NilBlock);
699 i = mkindices(&e, bn, index);
702 if(i > DEPTH(e.type)){
704 werrstr("bad address 0x%lux", (ulong)bn);
708 if(growdepth(r, b, &e, i) < 0)
712 assert(b->type == VtDirType);
714 index[DEPTH(e.type)] = r->offset % r->epb;
716 /* mode for intermediate block */
721 for(i=DEPTH(e.type); i>=0; i--){
722 bb = blockwalk(r, b, index[i], r->c, i==0 ? mode : m, &e);
728 b->pc = getcallerpc(&r);
736 vtfileblockscore(VtFile *r, u32int bn, uchar score[VtScoreSize])
739 int index[VtPointerDepth+1];
744 assert(bn != NilBlock);
750 if(DEPTH(e.type) == 0){
751 memmove(score, e.score, VtScoreSize);
756 i = mkindices(&e, bn, index);
761 if(i > DEPTH(e.type)){
762 memmove(score, vtzeroscore, VtScoreSize);
767 index[DEPTH(e.type)] = r->offset % r->epb;
769 for(i=DEPTH(e.type); i>=1; i--){
770 bb = blockwalk(r, b, index[i], r->c, VtOREAD, &e);
775 if(memcmp(b->score, vtzeroscore, VtScoreSize) == 0)
779 memmove(score, b->data+index[0]*VtScoreSize, VtScoreSize);
789 vtfileincref(VtFile *r)
797 vtfileclose(VtFile *r)
810 vtfileclose(r->parent);
811 memset(r, ~0, sizeof(*r));
816 * Retrieve the block containing the entry for r.
817 * If a snapshot has happened, we might need
818 * to get a new copy of the block. We avoid this
819 * in the common case by caching the score for
820 * the block and the last epoch in which it was valid.
822 * We use r->mode to tell the difference between active
823 * file system VtFiles (VtORDWR) and VtFiles for the
824 * snapshot file system (VtOREAD).
827 fileloadblock(VtFile *r, int mode)
837 assert(r->mode == VtORDWR);
839 b = vtcacheglobal(r->c, r->score, VtDirType, r->bsize);
842 b->pc = getcallerpc(&r);
845 assert(r->parent != nil);
846 if(vtfilelock(r->parent, VtORDWR) < 0)
848 b = vtfileblock(r->parent, r->offset/r->epb, VtORDWR);
849 vtfileunlock(r->parent);
852 memmove(r->score, b->score, VtScoreSize);
858 werrstr("read/write lock of read-only file");
861 addr = vtglobaltolocal(r->score);
863 return vtcacheglobal(r->c, r->score, VtDirType, r->bsize);
865 b = vtcachelocal(r->c, addr, VtDirType);
870 * If it failed because the epochs don't match, the block has been
871 * archived and reclaimed. Rewalk from the parent and get the
872 * new pointer. This can't happen in the VtORDWR case
873 * above because blocks in the current epoch don't get
874 * reclaimed. The fact that we're VtOREAD means we're
875 * a snapshot. (Or else the file system is read-only, but then
876 * the archiver isn't going around deleting blocks.)
878 rerrstr(e, sizeof e);
879 if(strcmp(e, ELabelMismatch) == 0){
880 if(vtfilelock(r->parent, VtOREAD) < 0)
882 b = vtfileblock(r->parent, r->offset/r->epb, VtOREAD);
883 vtfileunlock(r->parent);
885 fprint(2, "vtfilealloc: lost %V found %V\n",
887 memmove(r->score, b->score, VtScoreSize);
896 vtfilelock(VtFile *r, int mode)
903 b = fileloadblock(r, mode);
907 * The fact that we are holding b serves as the
908 * lock entitling us to write to r->b.
912 b->pc = getcallerpc(&r);
917 * Lock two (usually sibling) VtFiles. This needs special care
918 * because the Entries for both vtFiles might be in the same block.
919 * We also try to lock blocks in left-to-right order within the tree.
922 vtfilelock2(VtFile *r, VtFile *rr, int mode)
927 return vtfilelock(r, mode);
932 if(r->parent==rr->parent && r->offset/r->epb == rr->offset/rr->epb){
933 b = fileloadblock(r, mode);
938 }else if(r->parent==rr->parent || r->offset > rr->offset){
939 bb = fileloadblock(rr, mode);
940 b = fileloadblock(r, mode);
942 b = fileloadblock(r, mode);
943 bb = fileloadblock(rr, mode);
945 if(b == nil || bb == nil){
954 * The fact that we are holding b and bb serves
955 * as the lock entitling us to write to r->b and rr->b.
959 b->pc = getcallerpc(&r);
960 bb->pc = getcallerpc(&r);
965 vtfileunlock(VtFile *r)
970 fprint(2, "vtfileunlock: already unlocked\n");
979 fileload(VtFile *r, VtEntry *e)
985 if(vtentryunpack(e, b->data, r->offset % r->epb) < 0)
992 sizetodepth(uvlong s, int psize, int dsize)
997 /* determine pointer depth */
998 np = psize/VtScoreSize;
999 s = (s + dsize - 1)/dsize;
1000 for(d = 0; s > 1; d++)
1001 s = (s + np - 1)/np;
1006 vtfileread(VtFile *f, void *data, long count, vlong offset)
1012 assert(ISLOCKED(f));
1014 vtfilegetentry(f, &e);
1017 if(count < 0 || offset < 0){
1018 werrstr("vtfileread: bad offset or count");
1021 if(offset >= e.size)
1024 if(offset+count > e.size)
1025 count = e.size - offset;
1027 frag = offset % e.dsize;
1028 if(frag+count > e.dsize)
1029 count = e.dsize - frag;
1031 b = vtfileblock(f, offset/e.dsize, VtOREAD);
1035 memmove(data, b->data+frag, count);
1041 filewrite1(VtFile *f, void *data, long count, vlong offset)
1047 vtfilegetentry(f, &e);
1048 if(count < 0 || offset < 0){
1049 werrstr("vtfilewrite: bad offset or count");
1053 frag = offset % e.dsize;
1054 if(frag+count > e.dsize)
1055 count = e.dsize - frag;
1058 if(frag == 0 && count == e.dsize)
1061 b = vtfileblock(f, offset/e.dsize, m);
1065 memmove(b->data+frag, data, count);
1066 if(m == VtOWRITE && frag+count < e.dsize)
1067 memset(b->data+frag+count, 0, e.dsize-frag-count);
1069 if(offset+count > e.size){
1070 vtfilegetentry(f, &e);
1071 e.size = offset+count;
1072 vtfilesetentry(f, &e);
1080 vtfilewrite(VtFile *f, void *data, long count, vlong offset)
1084 assert(ISLOCKED(f));
1089 m = filewrite1(f, (char*)data+tot, count-tot, offset+tot);
1100 flushblock(VtCache *c, VtBlock *bb, uchar score[VtScoreSize], int ppb, int epb,
1108 addr = vtglobaltolocal(score);
1109 if(addr == NilBlock)
1114 if(memcmp(b->score, score, VtScoreSize) != 0)
1117 if((b = vtcachelocal(c, addr, type)) == nil)
1125 for(i=0; i<epb; i++){
1126 if(vtentryunpack(&e, b->data, i) < 0)
1128 if(!(e.flags&VtEntryActive))
1130 if(flushblock(c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize,
1133 vtentrypack(&e, b->data, i);
1137 default: /* VtPointerTypeX */
1138 for(i=0; i<ppb; i++){
1139 if(flushblock(c, nil, b->data+VtScoreSize*i, ppb, epb, type-1) < 0)
1145 if(vtblockwrite(b) < 0)
1147 memmove(score, b->score, VtScoreSize);
1159 vtfileflush(VtFile *f)
1165 assert(ISLOCKED(f));
1166 b = fileload(f, &e);
1167 if(!(e.flags&VtEntryLocal)){
1172 ret = flushblock(f->c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize,
1179 vtentrypack(&e, b->data, f->offset % f->epb);
1185 vtfileflushbefore(VtFile *r, u64int offset)
1189 int i, base, depth, ppb, epb, doflush;
1190 int index[VtPointerDepth+1], j, ret;
1191 VtBlock *bi[VtPointerDepth+2];
1194 assert(ISLOCKED(r));
1198 b = fileload(r, &e);
1203 * compute path through tree for the last written byte and the next one.
1206 memset(bi, 0, sizeof bi);
1207 depth = DEPTH(e.type);
1209 i = mkindices(&e, (offset-1)/e.dsize, index);
1214 ppb = e.psize / VtScoreSize;
1215 epb = e.dsize / VtEntrySize;
1218 * load the blocks along the last written byte
1220 index[depth] = r->offset % r->epb;
1221 for(i=depth; i>=0; i--){
1222 bb = blockwalk(r, b, index[i], r->c, VtORDWR, &e);
1231 * walk up the path from leaf to root, flushing anything that
1232 * has been finished.
1234 base = e.type&~VtTypeDepthMask;
1235 for(i=0; i<=depth; i++){
1238 /* leaf: data or dir block */
1239 if(offset%e.dsize == 0)
1243 * interior node: pointer blocks.
1244 * specifically, b = bi[i] is a block whose index[i-1]'th entry
1245 * points at bi[i-1].
1250 * the index entries up to but not including index[i-1] point at
1251 * finished blocks, so flush them for sure.
1253 for(j=0; j<index[i-1]; j++)
1254 if(flushblock(r->c, nil, b->data+j*VtScoreSize, ppb, epb, base+i-1) < 0)
1258 * if index[i-1] is the last entry in the block and is global
1259 * (i.e. the kid is flushed), then we can flush this block.
1261 if(j==ppb-1 && vtglobaltolocal(b->data+j*VtScoreSize)==NilBlock)
1268 score = bi[i+1]->data+index[i]*VtScoreSize;
1269 if(flushblock(r->c, bi[i], score, ppb, epb, base+i) < 0)
1275 /* top: entry. do this always so that the score is up-to-date */
1276 vtentrypack(&e, bi[depth+1]->data, index[depth]);
1277 for(i=0; i<nelem(bi); i++)