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 assert(b->addr != NilBlock);
105 memmove(r->score, b->score, VtScoreSize);
117 vtfileroot(VtCache *c, u32int addr, int mode)
122 b = vtcachelocal(c, addr, VtDirType);
125 r = vtfilealloc(c, b, nil, 0, mode);
131 vtfileopenroot(VtCache *c, VtEntry *e)
136 b = vtcacheallocblock(c, VtDirType);
140 vtentrypack(e, b->data, 0);
141 f = vtfilealloc(c, b, nil, 0, VtORDWR);
147 vtfilecreateroot(VtCache *c, int psize, int dsize, int type)
151 memset(&e, 0, sizeof e);
152 e.flags = VtEntryActive;
156 memmove(e.score, vtzeroscore, VtScoreSize);
158 return vtfileopenroot(c, &e);
162 vtfileopen(VtFile *r, u32int offset, int mode)
173 bn = offset/(r->dsize/VtEntrySize);
175 b = vtfileblock(r, bn, mode);
178 r = vtfilealloc(r->c, b, r, offset, mode);
184 vtfilecreate(VtFile *r, int psize, int dsize, int dir)
195 assert(psize <= VtMaxLumpSize);
196 assert(dsize <= VtMaxLumpSize);
203 epb = r->dsize/VtEntrySize;
205 size = vtfilegetdirsize(r);
207 * look at a random block to see if we can find an empty entry
209 offset = lnrand(size+1);
210 offset -= offset % epb;
212 /* try the given block and then try the last block */
215 b = vtfileblock(r, bn, VtORDWR);
218 for(i=offset%r->epb; i<epb; i++){
219 if(vtentryunpack(&e, b->data, i) < 0)
221 if((e.flags&VtEntryActive) == 0 && e.gen != ~0)
226 fprint(2, "vtfilecreate: cannot happen\n");
227 werrstr("vtfilecreate: cannot happen");
234 /* found an entry - gen already set */
237 e.flags = VtEntryActive;
238 e.type = dir ? VtDirType : VtDataType;
240 memmove(e.score, vtzeroscore, VtScoreSize);
241 vtentrypack(&e, b->data, i);
245 if(vtfilesetdirsize(r, offset+1) < 0){
251 rr = vtfilealloc(r->c, b, r, offset, VtORDWR);
257 vtfilekill(VtFile *r, int doremove)
267 if(doremove==0 && e.size == 0){
268 /* already truncated */
280 e.flags &= ~VtEntryLocal;
283 memmove(e.score, vtzeroscore, VtScoreSize);
284 vtentrypack(&e, b->data, r->offset % r->epb);
296 vtfileremove(VtFile *r)
298 return vtfilekill(r, 1);
302 vtfiletruncate(VtFile *r)
304 return vtfilekill(r, 0);
308 vtfilegetsize(VtFile *r)
323 shrinksize(VtFile *r, VtEntry *e, uvlong size)
325 int i, depth, type, isdir, ppb;
327 uchar score[VtScoreSize];
330 b = vtcacheglobal(r->c, e->score, e->type);
335 ppb = e->psize/VtScoreSize;
338 for(i=0; i+1<depth; i++)
343 if(b->addr == NilBlock){
344 /* not worth copying the block just so we can zero some of it */
350 * invariant: each pointer in the tree rooted at b accounts for ptrsz bytes
353 /* zero the pointers to unnecessary blocks */
354 i = (size+ptrsz-1)/ptrsz;
356 memmove(b->data+i*VtScoreSize, vtzeroscore, VtScoreSize);
358 /* recurse (go around again) on the partially necessary block */
367 memmove(score, b->data+i*VtScoreSize, VtScoreSize);
369 b = vtcacheglobal(r->c, score, type);
374 if(b->addr == NilBlock){
380 * No one ever truncates BtDir blocks.
382 if(depth==0 && !isdir && e->dsize > size)
383 memset(b->data+size, 0, e->dsize-size);
389 vtfilesetsize(VtFile *r, uvlong size)
397 return vtfiletruncate(r);
399 if(size > VtMaxFileSize || size > ((uvlong)MaxBlock)*r->dsize){
414 depth = sizetodepth(size, e.psize, e.dsize);
415 edepth = DEPTH(e.type);
417 if(shrinkdepth(r, b, &e, depth) < 0){
421 }else if(depth > edepth){
422 if(growdepth(r, b, &e, depth) < 0){
429 shrinksize(r, &e, size);
432 vtentrypack(&e, b->data, r->offset % r->epb);
439 vtfilesetdirsize(VtFile *r, u32int ds)
445 epb = r->dsize/VtEntrySize;
447 size = (uvlong)r->dsize*(ds/epb);
448 size += VtEntrySize*(ds%epb);
449 return vtfilesetsize(r, size);
453 vtfilegetdirsize(VtFile *r)
460 epb = r->dsize/VtEntrySize;
462 size = vtfilegetsize(r);
463 ds = epb*(size/r->dsize);
464 ds += (size%r->dsize)/VtEntrySize;
469 vtfilegetentry(VtFile *r, VtEntry *e)
483 vtfilesetentry(VtFile *r, VtEntry *e)
489 b = fileload(r, &ee);
492 vtentrypack(e, b->data, r->offset % r->epb);
498 blockwalk(VtBlock *p, int index, VtCache *c, int mode, VtEntry *e)
514 score = p->data+index*VtScoreSize;
517 //print("walk from %V/%d ty %d to %V ty %d\n", p->score, index, p->type, score, type);
519 if(mode == VtOWRITE && vtglobaltolocal(score) == NilBlock){
520 b = vtcacheallocblock(c, type);
524 b = vtcacheglobal(c, score, type);
526 if(b == nil || mode == VtOREAD)
529 if(vtglobaltolocal(b->score) != NilBlock)
537 e->flags |= VtEntryLocal;
539 b = vtblockcopy(b/*, e->tag, fs->ehi, fs->elo*/);
544 if(p->type == VtDirType){
545 memmove(e->score, b->score, VtScoreSize);
546 vtentrypack(e, p->data, index);
548 memmove(p->data+index*VtScoreSize, b->score, VtScoreSize);
554 * Change the depth of the VtFile r.
555 * The entry e for r is contained in block p.
558 growdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth)
564 assert(depth <= VtPointerDepth);
566 b = vtcacheglobal(r->c, e->score, e->type);
573 * Keep adding layers until we get to the right depth
574 * or an error occurs.
576 while(DEPTH(e->type) < depth){
577 bb = vtcacheallocblock(r->c, e->type+1);
580 memmove(bb->data, b->score, VtScoreSize);
581 memmove(e->score, bb->score, VtScoreSize);
583 e->flags |= VtEntryLocal;
588 vtentrypack(e, p->data, r->offset % r->epb);
591 if(DEPTH(e->type) == depth)
597 shrinkdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth)
599 VtBlock *b, *nb, *ob, *rb;
603 assert(depth <= VtPointerDepth);
605 rb = vtcacheglobal(r->c, e->score, e->type);
610 * Walk down to the new root block.
611 * We may stop early, but something is better than nothing.
617 for(; DEPTH(e->type) > depth; e->type--){
618 nb = vtcacheglobal(r->c, b->data, e->type-1);
621 if(ob!=nil && ob!=rb)
633 * Right now, e points at the root block rb, b is the new root block,
634 * and ob points at b. To update:
636 * (i) change e to point at b
637 * (ii) zero the pointer ob -> b
638 * (iii) free the root block
640 * p (the block containing e) must be written before
645 memmove(e->score, b->score, VtScoreSize);
646 vtentrypack(e, p->data, r->offset % r->epb);
649 memmove(ob->data, vtzeroscore, VtScoreSize);
653 if(ob!=nil && ob!=rb)
657 if(DEPTH(e->type) == depth)
663 mkindices(VtEntry *e, u32int bn, int *index)
667 memset(index, 0, (VtPointerDepth+1)*sizeof(int));
669 np = e->psize/VtScoreSize;
670 for(i=0; bn > 0; i++){
671 if(i >= VtPointerDepth){
672 werrstr("bad address 0x%lux", (ulong)bn);
682 vtfileblock(VtFile *r, u32int bn, int mode)
685 int index[VtPointerDepth+1];
691 assert(bn != NilBlock);
697 i = mkindices(&e, bn, index);
700 if(i > DEPTH(e.type)){
702 werrstr("bad address 0x%lux", (ulong)bn);
706 if(growdepth(r, b, &e, i) < 0)
710 assert(b->type == VtDirType);
712 index[DEPTH(e.type)] = r->offset % r->epb;
714 /* mode for intermediate block */
719 for(i=DEPTH(e.type); i>=0; i--){
720 bb = blockwalk(b, index[i], r->c, i==0 ? mode : m, &e);
733 vtfileblockscore(VtFile *r, u32int bn, uchar score[VtScoreSize])
736 int index[VtPointerDepth+1];
741 assert(bn != NilBlock);
747 i = mkindices(&e, bn, index);
752 if(i > DEPTH(e.type)){
753 memmove(score, vtzeroscore, VtScoreSize);
758 index[DEPTH(e.type)] = r->offset % r->epb;
760 for(i=DEPTH(e.type); i>=1; i--){
761 bb = blockwalk(b, index[i], r->c, VtOREAD, &e);
766 if(memcmp(b->score, vtzeroscore, VtScoreSize) == 0)
770 memmove(score, b->data+index[0]*VtScoreSize, VtScoreSize);
780 vtfileincref(VtFile *r)
788 vtfileclose(VtFile *r)
801 vtfileclose(r->parent);
802 memset(r, ~0, sizeof(*r));
807 * Retrieve the block containing the entry for r.
808 * If a snapshot has happened, we might need
809 * to get a new copy of the block. We avoid this
810 * in the common case by caching the score for
811 * the block and the last epoch in which it was valid.
813 * We use r->mode to tell the difference between active
814 * file system VtFiles (VtORDWR) and VtFiles for the
815 * snapshot file system (VtOREAD).
818 fileloadblock(VtFile *r, int mode)
828 assert(r->mode == VtORDWR);
830 b = vtcacheglobal(r->c, r->score, VtDirType);
835 assert(r->parent != nil);
836 if(vtfilelock(r->parent, VtORDWR) < 0)
838 b = vtfileblock(r->parent, r->offset/r->epb, VtORDWR);
839 vtfileunlock(r->parent);
842 memmove(r->score, b->score, VtScoreSize);
848 werrstr("read/write lock of read-only file");
851 addr = vtglobaltolocal(r->score);
853 return vtcacheglobal(r->c, r->score, VtDirType);
855 b = vtcachelocal(r->c, addr, VtDirType);
860 * If it failed because the epochs don't match, the block has been
861 * archived and reclaimed. Rewalk from the parent and get the
862 * new pointer. This can't happen in the VtORDWR case
863 * above because blocks in the current epoch don't get
864 * reclaimed. The fact that we're VtOREAD means we're
865 * a snapshot. (Or else the file system is read-only, but then
866 * the archiver isn't going around deleting blocks.)
868 rerrstr(e, sizeof e);
869 if(strcmp(e, ELabelMismatch) == 0){
870 if(vtfilelock(r->parent, VtOREAD) < 0)
872 b = vtfileblock(r->parent, r->offset/r->epb, VtOREAD);
873 vtfileunlock(r->parent);
875 fprint(2, "vtfilealloc: lost %V found %V\n",
877 memmove(r->score, b->score, VtScoreSize);
886 vtfilelock(VtFile *r, int mode)
893 b = fileloadblock(r, mode);
897 * The fact that we are holding b serves as the
898 * lock entitling us to write to r->b.
906 * Lock two (usually sibling) VtFiles. This needs special care
907 * because the Entries for both vtFiles might be in the same block.
908 * We also try to lock blocks in left-to-right order within the tree.
911 vtfilelock2(VtFile *r, VtFile *rr, int mode)
916 return vtfilelock(r, mode);
921 if(r->parent==rr->parent && r->offset/r->epb == rr->offset/rr->epb){
922 b = fileloadblock(r, mode);
927 }else if(r->parent==rr->parent || r->offset > rr->offset){
928 bb = fileloadblock(rr, mode);
929 b = fileloadblock(r, mode);
931 b = fileloadblock(r, mode);
932 bb = fileloadblock(rr, mode);
934 if(b == nil || bb == nil){
943 * The fact that we are holding b and bb serves
944 * as the lock entitling us to write to r->b and rr->b.
952 vtfileunlock(VtFile *r)
957 fprint(2, "vtfileunlock: already unlocked\n");
966 fileload(VtFile *r, VtEntry *e)
972 if(vtentryunpack(e, b->data, r->offset % r->epb) < 0)
979 sizetodepth(uvlong s, int psize, int dsize)
984 /* determine pointer depth */
985 np = psize/VtScoreSize;
986 s = (s + dsize - 1)/dsize;
987 for(d = 0; s > 1; d++)
993 vtfileread(VtFile *f, void *data, long count, vlong offset)
1001 vtfilegetentry(f, &e);
1004 if(count < 0 || offset < 0){
1005 werrstr("vtfileread: bad offset or count");
1008 if(offset >= e.size)
1011 if(offset+count > e.size)
1012 count = e.size - offset;
1014 frag = offset % e.dsize;
1015 if(frag+count > e.dsize)
1016 count = e.dsize - frag;
1018 b = vtfileblock(f, offset/e.dsize, VtOREAD);
1022 memmove(data, b->data+frag, count);
1028 filewrite1(VtFile *f, void *data, long count, vlong offset)
1034 vtfilegetentry(f, &e);
1035 if(count < 0 || offset < 0){
1036 werrstr("vtfilewrite: bad offset or count");
1040 frag = offset % e.dsize;
1041 if(frag+count > e.dsize)
1042 count = e.dsize - frag;
1045 if(frag == 0 && count == e.dsize)
1048 b = vtfileblock(f, offset/e.dsize, m);
1052 memmove(b->data+frag, data, count);
1054 if(offset+count > e.size){
1055 vtfilegetentry(f, &e);
1056 e.size = offset+count;
1057 vtfilesetentry(f, &e);
1065 vtfilewrite(VtFile *f, void *data, long count, vlong offset)
1069 assert(ISLOCKED(f));
1074 m = filewrite1(f, (char*)data+tot, count-tot, offset+tot);
1085 flushblock(VtCache *c, VtBlock *bb, uchar score[VtScoreSize], int ppb, int epb,
1093 addr = vtglobaltolocal(score);
1094 if(addr == NilBlock)
1099 if(memcmp(b->score, score, VtScoreSize) != 0)
1102 if((b = vtcachelocal(c, addr, type)) == nil)
1110 for(i=0; i<epb; i++){
1111 if(vtentryunpack(&e, b->data, i) < 0)
1113 if(flushblock(c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize,
1119 default: /* VtPointerTypeX */
1120 for(i=0; i<ppb; i++){
1121 if(flushblock(c, nil, b->data+VtScoreSize*i, ppb, epb, type-1) < 0)
1127 if(vtblockwrite(b) < 0)
1129 memmove(score, b->score, VtScoreSize);
1141 vtfileflush(VtFile *f)
1147 assert(ISLOCKED(f));
1148 b = fileload(f, &e);
1149 if(!(e.flags&VtEntryLocal)){
1154 ret = flushblock(f->c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize,
1161 vtentrypack(&e, b->data, f->offset % f->epb);
1167 vtfileflushbefore(VtFile *r, u64int offset)
1171 int i, base, depth, ppb, epb, doflush;
1172 int index[VtPointerDepth+1], j, ret;
1173 VtBlock *bi[VtPointerDepth+2];
1176 assert(ISLOCKED(r));
1180 b = fileload(r, &e);
1185 * compute path through tree for the last written byte and the next one.
1188 memset(bi, 0, sizeof bi);
1189 depth = DEPTH(e.type);
1191 i = mkindices(&e, (offset-1)/e.dsize, index);
1196 ppb = e.psize / VtScoreSize;
1197 epb = e.dsize / VtEntrySize;
1200 * load the blocks along the last written byte
1202 index[depth] = r->offset % r->epb;
1203 for(i=depth; i>=0; i--){
1204 bb = blockwalk(b, index[i], r->c, VtORDWR, &e);
1213 * walk up the path from leaf to root, flushing anything that
1214 * has been finished.
1216 base = e.type&~VtTypeDepthMask;
1217 for(i=0; i<=depth; i++){
1220 /* leaf: data or dir block */
1221 if(offset%e.dsize == 0)
1225 * interior node: pointer blocks.
1226 * specifically, b = bi[i] is a block whose index[i-1]'th entry
1227 * points at bi[i-1].
1232 * the index entries up to but not including index[i-1] point at
1233 * finished blocks, so flush them for sure.
1235 for(j=0; j<index[i-1]; j++)
1236 if(flushblock(r->c, nil, b->data+j*VtScoreSize, ppb, epb, base+i-1) < 0)
1240 * if index[i-1] is the last entry in the block and is global
1241 * (i.e. the kid is flushed), then we can flush this block.
1243 if(j==ppb-1 && vtglobaltolocal(b->data+j*VtScoreSize)==NilBlock)
1250 score = bi[i+1]->data+index[i]*VtScoreSize;
1251 if(flushblock(r->c, bi[i], score, ppb, epb, base+i) < 0)
1257 /* top: entry. do this always so that the score is up-to-date */
1258 vtentrypack(&e, bi[depth+1]->data, index[depth]);
1259 for(i=0; i<nelem(bi); i++)