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 EBadEntry[] = "bad VtEntry";
22 static char ENotDir[] = "walk in non-directory";
23 static char ETooBig[] = "file too big";
24 /* static char EBadAddr[] = "bad address"; */
25 static char ELabelMismatch[] = "label mismatch";
27 static int sizetodepth(uvlong s, int psize, int dsize);
28 static VtBlock *fileload(VtFile *r, VtEntry *e);
29 static int shrinkdepth(VtFile*, VtBlock*, VtEntry*, int);
30 static int shrinksize(VtFile*, VtEntry*, uvlong);
31 static int growdepth(VtFile*, VtBlock*, VtEntry*, int);
33 #define ISLOCKED(r) ((r)->b != nil)
34 #define DEPTH(t) ((t)&VtTypeDepthMask)
37 vtfilealloc(VtCache *c, VtBlock *b, VtFile *p, u32int offset, int mode)
44 assert(p==nil || ISLOCKED(p));
50 epb = p->dsize / VtEntrySize;
52 if(b->type != VtDirType)
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\n");
64 if(!(e.flags & VtEntryActive)){
65 if(0)fprint(2, "not active\n");
68 if(e.psize < 256 || e.dsize < 256){
69 fprint(2, "psize %ud dsize %ud\n", e.psize, e.dsize);
73 if(DEPTH(e.type) < sizetodepth(e.size, e.psize, e.dsize)){
74 fprint(2, "depth %ud size %llud psize %ud dsize %ud\n",
75 DEPTH(e.type), e.size, e.psize, e.dsize);
79 size = vtcacheblocksize(c);
80 if(e.dsize > size || e.psize > size){
81 fprint(2, "psize %ud dsize %ud blocksize %ud\n", e.psize, e.dsize, size);
85 r = vtmallocz(sizeof(VtFile));
90 r->dir = (e.type & VtTypeBaseMask) == VtDirType;
95 assert(mode == VtOREAD || p->mode == VtORDWR);
99 assert(b->addr != NilBlock);
102 memmove(r->score, b->score, VtScoreSize);
114 vtfileroot(VtCache *c, u32int addr, int mode)
119 b = vtcachelocal(c, addr, VtDirType);
122 r = vtfilealloc(c, b, nil, 0, mode);
128 vtfileopenroot(VtCache *c, VtEntry *e)
133 b = vtcacheallocblock(c, VtDirType);
137 vtentrypack(e, b->data, 0);
138 f = vtfilealloc(c, b, nil, 0, VtORDWR);
144 vtfilecreateroot(VtCache *c, int psize, int dsize, int type)
148 memset(&e, 0, sizeof e);
149 e.flags = VtEntryActive;
153 memmove(e.score, vtzeroscore, VtScoreSize);
155 return vtfileopenroot(c, &e);
159 vtfileopen(VtFile *r, u32int offset, int mode)
170 bn = offset/(r->dsize/VtEntrySize);
172 b = vtfileblock(r, bn, mode);
175 r = vtfilealloc(r->c, b, r, offset, mode);
181 vtfilecreate(VtFile *r, int psize, int dsize, int dir)
192 assert(psize <= VtMaxLumpSize);
193 assert(dsize <= VtMaxLumpSize);
200 epb = r->dsize/VtEntrySize;
202 size = vtfilegetdirsize(r);
204 * look at a random block to see if we can find an empty entry
206 offset = lnrand(size+1);
207 offset -= offset % epb;
209 /* try the given block and then try the last block */
212 b = vtfileblock(r, bn, VtORDWR);
215 for(i=offset%r->epb; i<epb; i++){
216 if(vtentryunpack(&e, b->data, i) < 0)
218 if((e.flags&VtEntryActive) == 0 && e.gen != ~0)
223 fprint(2, "vtfilecreate: cannot happen\n");
224 werrstr("vtfilecreate: cannot happen");
231 /* found an entry - gen already set */
234 e.flags = VtEntryActive;
235 e.type = dir ? VtDirType : VtDataType;
237 memmove(e.score, vtzeroscore, VtScoreSize);
238 vtentrypack(&e, b->data, i);
242 if(vtfilesetdirsize(r, offset+1) < 0){
248 rr = vtfilealloc(r->c, b, r, offset, VtORDWR);
254 vtfilekill(VtFile *r, int doremove)
264 if(doremove==0 && e.size == 0){
265 /* already truncated */
277 e.flags &= ~VtEntryLocal;
280 memmove(e.score, vtzeroscore, VtScoreSize);
281 vtentrypack(&e, b->data, r->offset % r->epb);
293 vtfileremove(VtFile *r)
295 return vtfilekill(r, 1);
299 vtfiletruncate(VtFile *r)
301 return vtfilekill(r, 0);
305 vtfilegetsize(VtFile *r)
320 shrinksize(VtFile *r, VtEntry *e, uvlong size)
322 int i, depth, type, isdir, ppb;
324 uchar score[VtScoreSize];
327 b = vtcacheglobal(r->c, e->score, e->type);
332 ppb = e->psize/VtScoreSize;
335 for(i=0; i+1<depth; i++)
340 if(b->addr == NilBlock){
341 /* not worth copying the block just so we can zero some of it */
347 * invariant: each pointer in the tree rooted at b accounts for ptrsz bytes
350 /* zero the pointers to unnecessary blocks */
351 i = (size+ptrsz-1)/ptrsz;
353 memmove(b->data+i*VtScoreSize, vtzeroscore, VtScoreSize);
355 /* recurse (go around again) on the partially necessary block */
364 memmove(score, b->data+i*VtScoreSize, VtScoreSize);
366 b = vtcacheglobal(r->c, score, type);
371 if(b->addr == NilBlock){
377 * No one ever truncates BtDir blocks.
379 if(depth==0 && !isdir && e->dsize > size)
380 memset(b->data+size, 0, e->dsize-size);
386 vtfilesetsize(VtFile *r, uvlong size)
394 return vtfiletruncate(r);
396 if(size > VtMaxFileSize || size > ((uvlong)MaxBlock)*r->dsize){
411 depth = sizetodepth(size, e.psize, e.dsize);
412 edepth = DEPTH(e.type);
414 if(shrinkdepth(r, b, &e, depth) < 0){
418 }else if(depth > edepth){
419 if(growdepth(r, b, &e, depth) < 0){
426 shrinksize(r, &e, size);
429 vtentrypack(&e, b->data, r->offset % r->epb);
436 vtfilesetdirsize(VtFile *r, u32int ds)
442 epb = r->dsize/VtEntrySize;
444 size = (uvlong)r->dsize*(ds/epb);
445 size += VtEntrySize*(ds%epb);
446 return vtfilesetsize(r, size);
450 vtfilegetdirsize(VtFile *r)
457 epb = r->dsize/VtEntrySize;
459 size = vtfilegetsize(r);
460 ds = epb*(size/r->dsize);
461 ds += (size%r->dsize)/VtEntrySize;
466 vtfilegetentry(VtFile *r, VtEntry *e)
480 vtfilesetentry(VtFile *r, VtEntry *e)
486 b = fileload(r, &ee);
489 vtentrypack(e, b->data, r->offset % r->epb);
495 blockwalk(VtBlock *p, int index, VtCache *c, int mode, VtEntry *e)
511 score = p->data+index*VtScoreSize;
514 //print("walk from %V/%d ty %d to %V ty %d\n", p->score, index, p->type, score, type);
516 if(mode == VtOWRITE && vtglobaltolocal(score) == NilBlock){
517 b = vtcacheallocblock(c, type);
521 b = vtcacheglobal(c, score, type);
523 if(b == nil || mode == VtOREAD)
526 if(vtglobaltolocal(b->score) != NilBlock)
534 e->flags |= VtEntryLocal;
536 b = vtblockcopy(b/*, e->tag, fs->ehi, fs->elo*/);
541 if(p->type == VtDirType){
542 memmove(e->score, b->score, VtScoreSize);
543 vtentrypack(e, p->data, index);
545 memmove(p->data+index*VtScoreSize, b->score, VtScoreSize);
551 * Change the depth of the VtFile r.
552 * The entry e for r is contained in block p.
555 growdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth)
561 assert(depth <= VtPointerDepth);
563 b = vtcacheglobal(r->c, e->score, e->type);
570 * Keep adding layers until we get to the right depth
571 * or an error occurs.
573 while(DEPTH(e->type) < depth){
574 bb = vtcacheallocblock(r->c, e->type+1);
577 memmove(bb->data, b->score, VtScoreSize);
578 memmove(e->score, bb->score, VtScoreSize);
580 e->flags |= VtEntryLocal;
585 vtentrypack(e, p->data, r->offset % r->epb);
588 if(DEPTH(e->type) == depth)
594 shrinkdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth)
596 VtBlock *b, *nb, *ob, *rb;
600 assert(depth <= VtPointerDepth);
602 rb = vtcacheglobal(r->c, e->score, e->type);
607 * Walk down to the new root block.
608 * We may stop early, but something is better than nothing.
614 for(; DEPTH(e->type) > depth; e->type--){
615 nb = vtcacheglobal(r->c, b->data, e->type-1);
618 if(ob!=nil && ob!=rb)
630 * Right now, e points at the root block rb, b is the new root block,
631 * and ob points at b. To update:
633 * (i) change e to point at b
634 * (ii) zero the pointer ob -> b
635 * (iii) free the root block
637 * p (the block containing e) must be written before
642 memmove(e->score, b->score, VtScoreSize);
643 vtentrypack(e, p->data, r->offset % r->epb);
646 memmove(ob->data, vtzeroscore, VtScoreSize);
650 if(ob!=nil && ob!=rb)
654 if(DEPTH(e->type) == depth)
660 mkindices(VtEntry *e, u32int bn, int *index)
664 memset(index, 0, (VtPointerDepth+1)*sizeof(int));
666 np = e->psize/VtScoreSize;
667 for(i=0; bn > 0; i++){
668 if(i >= VtPointerDepth){
669 werrstr("bad address 0x%lux", (ulong)bn);
679 vtfileblock(VtFile *r, u32int bn, int mode)
682 int index[VtPointerDepth+1];
688 assert(bn != NilBlock);
694 i = mkindices(&e, bn, index);
697 if(i > DEPTH(e.type)){
699 werrstr("bad address 0x%lux", (ulong)bn);
703 if(growdepth(r, b, &e, i) < 0)
707 assert(b->type == VtDirType);
709 index[DEPTH(e.type)] = r->offset % r->epb;
711 /* mode for intermediate block */
716 for(i=DEPTH(e.type); i>=0; i--){
717 bb = blockwalk(b, index[i], r->c, i==0 ? mode : m, &e);
730 vtfileblockscore(VtFile *r, u32int bn, uchar score[VtScoreSize])
733 int index[VtPointerDepth+1];
738 assert(bn != NilBlock);
744 i = mkindices(&e, bn, index);
749 if(i > DEPTH(e.type)){
750 memmove(score, vtzeroscore, VtScoreSize);
755 index[DEPTH(e.type)] = r->offset % r->epb;
757 for(i=DEPTH(e.type); i>=1; i--){
758 bb = blockwalk(b, index[i], r->c, VtOREAD, &e);
763 if(memcmp(b->score, vtzeroscore, VtScoreSize) == 0)
767 memmove(score, b->data+index[0]*VtScoreSize, VtScoreSize);
777 vtfileincref(VtFile *r)
785 vtfileclose(VtFile *r)
798 vtfileclose(r->parent);
799 memset(r, ~0, sizeof(*r));
804 * Retrieve the block containing the entry for r.
805 * If a snapshot has happened, we might need
806 * to get a new copy of the block. We avoid this
807 * in the common case by caching the score for
808 * the block and the last epoch in which it was valid.
810 * We use r->mode to tell the difference between active
811 * file system VtFiles (VtORDWR) and VtFiles for the
812 * snapshot file system (VtOREAD).
815 fileloadblock(VtFile *r, int mode)
825 assert(r->mode == VtORDWR);
827 b = vtcacheglobal(r->c, r->score, VtDirType);
832 assert(r->parent != nil);
833 if(vtfilelock(r->parent, VtORDWR) < 0)
835 b = vtfileblock(r->parent, r->offset/r->epb, VtORDWR);
836 vtfileunlock(r->parent);
839 memmove(r->score, b->score, VtScoreSize);
845 werrstr("read/write lock of read-only file");
848 addr = vtglobaltolocal(r->score);
850 return vtcacheglobal(r->c, r->score, VtDirType);
852 b = vtcachelocal(r->c, addr, VtDirType);
857 * If it failed because the epochs don't match, the block has been
858 * archived and reclaimed. Rewalk from the parent and get the
859 * new pointer. This can't happen in the VtORDWR case
860 * above because blocks in the current epoch don't get
861 * reclaimed. The fact that we're VtOREAD means we're
862 * a snapshot. (Or else the file system is read-only, but then
863 * the archiver isn't going around deleting blocks.)
865 rerrstr(e, sizeof e);
866 if(strcmp(e, ELabelMismatch) == 0){
867 if(vtfilelock(r->parent, VtOREAD) < 0)
869 b = vtfileblock(r->parent, r->offset/r->epb, VtOREAD);
870 vtfileunlock(r->parent);
872 fprint(2, "vtfilealloc: lost %V found %V\n",
874 memmove(r->score, b->score, VtScoreSize);
883 vtfilelock(VtFile *r, int mode)
890 b = fileloadblock(r, mode);
894 * The fact that we are holding b serves as the
895 * lock entitling us to write to r->b.
903 * Lock two (usually sibling) VtFiles. This needs special care
904 * because the Entries for both vtFiles might be in the same block.
905 * We also try to lock blocks in left-to-right order within the tree.
908 vtfilelock2(VtFile *r, VtFile *rr, int mode)
913 return vtfilelock(r, mode);
918 if(r->parent==rr->parent && r->offset/r->epb == rr->offset/rr->epb){
919 b = fileloadblock(r, mode);
924 }else if(r->parent==rr->parent || r->offset > rr->offset){
925 bb = fileloadblock(rr, mode);
926 b = fileloadblock(r, mode);
928 b = fileloadblock(r, mode);
929 bb = fileloadblock(rr, mode);
931 if(b == nil || bb == nil){
940 * The fact that we are holding b and bb serves
941 * as the lock entitling us to write to r->b and rr->b.
949 vtfileunlock(VtFile *r)
954 fprint(2, "vtfileunlock: already unlocked\n");
963 fileload(VtFile *r, VtEntry *e)
969 if(vtentryunpack(e, b->data, r->offset % r->epb) < 0)
976 sizetodepth(uvlong s, int psize, int dsize)
981 /* determine pointer depth */
982 np = psize/VtScoreSize;
983 s = (s + dsize - 1)/dsize;
984 for(d = 0; s > 1; d++)
990 vtfileread(VtFile *f, void *data, long count, vlong offset)
998 vtfilegetentry(f, &e);
1001 if(count < 0 || offset < 0){
1002 werrstr("vtfileread: bad offset or count");
1005 if(offset >= e.size)
1008 if(offset+count > e.size)
1009 count = e.size - offset;
1011 frag = offset % e.dsize;
1012 if(frag+count > e.dsize)
1013 count = e.dsize - frag;
1015 b = vtfileblock(f, offset/e.dsize, VtOREAD);
1019 memmove(data, b->data+frag, count);
1025 filewrite1(VtFile *f, void *data, long count, vlong offset)
1031 vtfilegetentry(f, &e);
1032 if(count < 0 || offset < 0){
1033 werrstr("vtfilewrite: bad offset or count");
1037 frag = offset % e.dsize;
1038 if(frag+count > e.dsize)
1039 count = e.dsize - frag;
1042 if(frag == 0 && count == e.dsize)
1045 b = vtfileblock(f, offset/e.dsize, m);
1049 memmove(b->data+frag, data, count);
1051 if(offset+count > e.size){
1052 vtfilegetentry(f, &e);
1053 e.size = offset+count;
1054 vtfilesetentry(f, &e);
1062 vtfilewrite(VtFile *f, void *data, long count, vlong offset)
1066 assert(ISLOCKED(f));
1071 m = filewrite1(f, (char*)data+tot, count-tot, offset+tot);
1082 flushblock(VtCache *c, VtBlock *bb, uchar score[VtScoreSize], int ppb, int epb,
1090 addr = vtglobaltolocal(score);
1091 if(addr == NilBlock)
1096 if(memcmp(b->score, score, VtScoreSize) != 0)
1099 if((b = vtcachelocal(c, addr, type)) == nil)
1107 for(i=0; i<epb; i++){
1108 if(vtentryunpack(&e, b->data, i) < 0)
1110 if(flushblock(c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize,
1116 default: /* VtPointerTypeX */
1117 for(i=0; i<ppb; i++){
1118 if(flushblock(c, nil, b->data+VtScoreSize*i, ppb, epb, type-1) < 0)
1124 if(vtblockwrite(b) < 0)
1126 memmove(score, b->score, VtScoreSize);
1138 vtfileflush(VtFile *f)
1144 assert(ISLOCKED(f));
1145 b = fileload(f, &e);
1146 if(!(e.flags&VtEntryLocal)){
1151 ret = flushblock(f->c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize,
1158 vtentrypack(&e, b->data, f->offset % f->epb);
1164 vtfileflushbefore(VtFile *r, u64int offset)
1168 int i, base, depth, ppb, epb, doflush;
1169 int index[VtPointerDepth+1], j, ret;
1170 VtBlock *bi[VtPointerDepth+2];
1173 assert(ISLOCKED(r));
1177 b = fileload(r, &e);
1182 * compute path through tree for the last written byte and the next one.
1185 memset(bi, 0, sizeof bi);
1186 depth = DEPTH(e.type);
1188 i = mkindices(&e, (offset-1)/e.dsize, index);
1193 ppb = e.psize / VtScoreSize;
1194 epb = e.dsize / VtEntrySize;
1197 * load the blocks along the last written byte
1199 index[depth] = r->offset % r->epb;
1200 for(i=depth; i>=0; i--){
1201 bb = blockwalk(b, index[i], r->c, VtORDWR, &e);
1210 * walk up the path from leaf to root, flushing anything that
1211 * has been finished.
1213 base = e.type&~VtTypeDepthMask;
1214 for(i=0; i<=depth; i++){
1217 /* leaf: data or dir block */
1218 if(offset%e.dsize == 0)
1222 * interior node: pointer blocks.
1223 * specifically, b = bi[i] is a block whose index[i-1]'th entry
1224 * points at bi[i-1].
1229 * the index entries up to but not including index[i-1] point at
1230 * finished blocks, so flush them for sure.
1232 for(j=0; j<index[i-1]; j++)
1233 if(flushblock(r->c, nil, b->data+j*VtScoreSize, ppb, epb, base+i-1) < 0)
1237 * if index[i-1] is the last entry in the block and is global
1238 * (i.e. the kid is flushed), then we can flush this block.
1240 if(j==ppb-1 && vtglobaltolocal(b->data+j*VtScoreSize)==NilBlock)
1247 score = bi[i+1]->data+index[i]*VtScoreSize;
1248 if(flushblock(r->c, bi[i], score, ppb, epb, base+i) < 0)
1254 /* top: entry. do this always so that the score is up-to-date */
1255 vtentrypack(&e, bi[depth+1]->data, index[depth]);
1256 for(i=0; i<nelem(bi); i++)