int vtfileflush(VtFile *f) { int ret; VtBlock *b; VtEntry e; assert(ISLOCKED(f)); b = fileload(f, &e); if(!(e.flags&VtEntryLocal)){ vtblockput(b); return 0; } ret = flushblock(f->c, nil, e.score, e.psize/VtScoreSize, e.dsize/VtEntrySize, e.type); if(ret < 0){ vtblockput(b); return -1; } vtentrypack(&e, b->data, f->offset % f->epb); vtblockput(b); return 0; }
long vtfileread(VtFile *f, void *data, long count, vlong offset) { int frag; VtBlock *b; VtEntry e; assert(ISLOCKED(f)); vtfilegetentry(f, &e); if(count == 0) return 0; if(count < 0 || offset < 0){ werrstr("vtfileread: bad offset or count"); return -1; } if(offset >= e.size) return 0; if(offset+count > e.size) count = e.size - offset; frag = offset % e.dsize; if(frag+count > e.dsize) count = e.dsize - frag; b = vtfileblock(f, offset/e.dsize, VtOREAD); if(b == nil) return -1; memmove(data, b->data+frag, count); vtblockput(b); return count; }
/* * Change the depth of the VtFile r. * The entry e for r is contained in block p. */ static int growdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth) { VtBlock *b, *bb; assert(ISLOCKED(r)); assert(depth <= VtPointerDepth); b = vtcacheglobal(r->c, e->score, e->type); if(b == nil) return -1; /* * Keep adding layers until we get to the right depth * or an error occurs. */ while(DEPTH(e->type) < depth){ bb = vtcacheallocblock(r->c, e->type+1); if(bb == nil) break; memmove(bb->data, b->score, VtScoreSize); memmove(e->score, bb->score, VtScoreSize); e->type++; e->flags |= VtEntryLocal; vtblockput(b); b = bb; } vtentrypack(e, p->data, r->offset % r->epb); vtblockput(b); if(DEPTH(e->type) == depth) return 0; return -1; }
VtBlock * vtfileblock(VtFile *r, u32int bn, int mode) { VtBlock *b, *bb; int index[VtPointerDepth+1]; VtEntry e; int i; int m; assert(ISLOCKED(r)); assert(bn != NilBlock); b = fileload(r, &e); if(b == nil) return nil; i = mkindices(&e, bn, index); if(i < 0) goto Err; if(i > DEPTH(e.type)){ if(mode == VtOREAD){ werrstr("bad address 0x%lux", (ulong)bn); goto Err; } index[i] = 0; if(growdepth(r, b, &e, i) < 0) goto Err; } assert(b->type == VtDirType); index[DEPTH(e.type)] = r->offset % r->epb; /* mode for intermediate block */ m = mode; /* why is this here? * if(m == VtOWRITE) m = VtORDWR; * well we disable it */ for(i=DEPTH(e.type); i>=0; i--){ bb = blockwalk(b, index[i], r->c, i==0 ? mode : m, &e); if(bb == nil) goto Err; vtblockput(b); b = bb; } b->pc = getcallerpc(&r); return b; Err: vtblockput(b); return nil; }
static VtBlock* fileload(VtFile *r, VtEntry *e) { VtBlock *b; assert(ISLOCKED(r)); b = r->b; if(vtentryunpack(e, b->data, r->offset % r->epb) < 0) return nil; vtblockduplock(b); return b; }
int vtfileblockscore(VtFile *r, u32int bn, uchar score[VtScoreSize]) { VtBlock *b, *bb; int index[VtPointerDepth+1]; VtEntry e; int i; assert(ISLOCKED(r)); assert(bn != NilBlock); b = fileload(r, &e); if(b == nil) return -1; if(DEPTH(e.type) == 0){ memmove(score, e.score, VtScoreSize); vtblockput(b); return 0; } i = mkindices(&e, bn, index); if(i < 0){ vtblockput(b); return -1; } if(i > DEPTH(e.type)){ memmove(score, vtzeroscore, VtScoreSize); vtblockput(b); return 0; } index[DEPTH(e.type)] = r->offset % r->epb; for(i=DEPTH(e.type); i>=1; i--){ bb = blockwalk(b, index[i], r->c, VtOREAD, &e); if(bb == nil) goto Err; vtblockput(b); b = bb; if(memcmp(b->score, vtzeroscore, VtScoreSize) == 0) break; } memmove(score, b->data+index[0]*VtScoreSize, VtScoreSize); vtblockput(b); return 0; Err: vtblockput(b); return -1; }
int vtfilesetdirsize(VtFile *r, uint32_t ds) { uint64_t size; int epb; assert(ISLOCKED(r)); epb = r->dsize/VtEntrySize; size = (uint64_t)r->dsize*(ds/epb); size += VtEntrySize*(ds%epb); return vtfilesetsize(r, size); }
int vtfilegetentry(VtFile *r, VtEntry *e) { VtBlock *b; assert(ISLOCKED(r)); b = fileload(r, e); if(b == nil) return -1; vtblockput(b); return 0; }
int vtfilesetdirsize(VtFile *r, u32int ds) { uvlong size; int epb; assert(ISLOCKED(r)); epb = r->dsize/VtEntrySize; size = (uvlong)r->dsize*(ds/epb); size += VtEntrySize*(ds%epb); return vtfilesetsize(r, size); }
uvlong vtfilegetsize(VtFile *r) { VtEntry e; VtBlock *b; assert(ISLOCKED(r)); b = fileload(r, &e); if(b == nil) return ~(uvlong)0; vtblockput(b); return e.size; }
int vtfilesetentry(VtFile *r, VtEntry *e) { VtBlock *b; VtEntry ee; assert(ISLOCKED(r)); b = fileload(r, &ee); if(b == nil) return -1; vtentrypack(e, b->data, r->offset % r->epb); vtblockput(b); return 0; }
int vtfilesetsize(VtFile *r, u64int size) { int depth, edepth; VtEntry e; VtBlock *b; assert(ISLOCKED(r)); if(size == 0) return vtfiletruncate(r); if(size > VtMaxFileSize || size > ((uvlong)MaxBlock)*r->dsize){ werrstr(ETooBig); return -1; } b = fileload(r, &e); if(b == nil) return -1; /* quick out */ if(e.size == size){ vtblockput(b); return 0; } depth = sizetodepth(size, e.psize, e.dsize); edepth = DEPTH(e.type); if(depth < edepth){ if(shrinkdepth(r, b, &e, depth) < 0){ vtblockput(b); return -1; } }else if(depth > edepth){ if(growdepth(r, b, &e, depth) < 0){ vtblockput(b); return -1; } } if(size < e.size) shrinksize(r, &e, size); e.size = size; vtentrypack(&e, b->data, r->offset % r->epb); vtblockput(b); return 0; }
u32int vtfilegetdirsize(VtFile *r) { ulong ds; uvlong size; int epb; assert(ISLOCKED(r)); epb = r->dsize/VtEntrySize; size = vtfilegetsize(r); ds = epb*(size/r->dsize); ds += (size%r->dsize)/VtEntrySize; return ds; }
uint32_t vtfilegetdirsize(VtFile *r) { uint32_t ds; uint64_t size; int epb; assert(ISLOCKED(r)); epb = r->dsize/VtEntrySize; size = vtfilegetsize(r); ds = epb*(size/r->dsize); ds += (size%r->dsize)/VtEntrySize; return ds; }
long vtfilewrite(VtFile *f, void *data, long count, vlong offset) { long tot, m; assert(ISLOCKED(f)); tot = 0; m = 0; while(tot < count){ m = filewrite1(f, (char*)data+tot, count-tot, offset+tot); if(m <= 0) break; tot += m; } if(tot==0) return m; return tot; }
int32_t vtfilewrite(VtFile *f, void *data, int32_t count, int64_t offset) { int32_t tot, m; assert(ISLOCKED(f)); tot = 0; m = 0; while(tot < count){ m = filewrite1(f, (char*)data+tot, count-tot, offset+tot); if(m <= 0) break; tot += m; } if(tot==0) return m; return tot; }
VtFile * vtfileopen(VtFile *r, u32int offset, int mode) { ulong bn; VtBlock *b; assert(ISLOCKED(r)); if(!r->dir){ werrstr(ENotDir); return nil; } bn = offset/(r->dsize/VtEntrySize); b = vtfileblock(r, bn, mode); if(b == nil) return nil; r = vtfilealloc(r->c, b, r, offset, mode); vtblockput(b); return r; }
static int vtfilekill(VtFile *r, int doremove) { VtEntry e; VtBlock *b; assert(ISLOCKED(r)); b = fileload(r, &e); if(b == nil) return -1; if(doremove==0 && e.size == 0){ /* already truncated */ vtblockput(b); return 0; } if(doremove){ if(e.gen != ~0) e.gen++; e.dsize = 0; e.psize = 0; e.flags = 0; }else e.flags &= ~VtEntryLocal; e.type = 0; e.size = 0; memmove(e.score, vtzeroscore, VtScoreSize); vtentrypack(&e, b->data, r->offset % r->epb); vtblockput(b); if(doremove){ vtfileunlock(r); vtfileclose(r); } return 0; }
int vtfileflushbefore(VtFile *r, u64int offset) { VtBlock *b, *bb; VtEntry e; int i, base, depth, ppb, epb, doflush; int index[VtPointerDepth+1], j, ret; VtBlock *bi[VtPointerDepth+2]; uchar *score; assert(ISLOCKED(r)); if(offset == 0) return 0; b = fileload(r, &e); if(b == nil) return -1; /* * compute path through tree for the last written byte and the next one. */ ret = -1; memset(bi, 0, sizeof bi); depth = DEPTH(e.type); bi[depth+1] = b; i = mkindices(&e, (offset-1)/e.dsize, index); if(i < 0) goto Err; if(i > depth) goto Err; ppb = e.psize / VtScoreSize; epb = e.dsize / VtEntrySize; /* * load the blocks along the last written byte */ index[depth] = r->offset % r->epb; for(i=depth; i>=0; i--){ bb = blockwalk(b, index[i], r->c, VtORDWR, &e); if(bb == nil) goto Err; bi[i] = bb; b = bb; } ret = 0; /* * walk up the path from leaf to root, flushing anything that * has been finished. */ base = e.type&~VtTypeDepthMask; for(i=0; i<=depth; i++){ doflush = 0; if(i == 0){ /* leaf: data or dir block */ if(offset%e.dsize == 0) doflush = 1; }else{ /* * interior node: pointer blocks. * specifically, b = bi[i] is a block whose index[i-1]'th entry * points at bi[i-1]. */ b = bi[i]; /* * the index entries up to but not including index[i-1] point at * finished blocks, so flush them for sure. */ for(j=0; j<index[i-1]; j++) if(flushblock(r->c, nil, b->data+j*VtScoreSize, ppb, epb, base+i-1) < 0) goto Err; /* * if index[i-1] is the last entry in the block and is global * (i.e. the kid is flushed), then we can flush this block. */ if(j==ppb-1 && vtglobaltolocal(b->data+j*VtScoreSize)==NilBlock) doflush = 1; } if(doflush){ if(i == depth) score = e.score; else score = bi[i+1]->data+index[i]*VtScoreSize; if(flushblock(r->c, bi[i], score, ppb, epb, base+i) < 0) goto Err; } } Err: /* top: entry. do this always so that the score is up-to-date */ vtentrypack(&e, bi[depth+1]->data, index[depth]); for(i=0; i<nelem(bi); i++) if(bi[i]) vtblockput(bi[i]); return ret; }
static int shrinkdepth(VtFile *r, VtBlock *p, VtEntry *e, int depth) { VtBlock *b, *nb, *ob, *rb; VtEntry oe; assert(ISLOCKED(r)); assert(depth <= VtPointerDepth); rb = vtcacheglobal(r->c, e->score, e->type); if(rb == nil) return -1; /* * Walk down to the new root block. * We may stop early, but something is better than nothing. */ oe = *e; ob = nil; b = rb; for(; DEPTH(e->type) > depth; e->type--){ nb = vtcacheglobal(r->c, b->data, e->type-1); if(nb == nil) break; if(ob!=nil && ob!=rb) vtblockput(ob); ob = b; b = nb; } if(b == rb){ vtblockput(rb); return 0; } /* * Right now, e points at the root block rb, b is the new root block, * and ob points at b. To update: * * (i) change e to point at b * (ii) zero the pointer ob -> b * (iii) free the root block * * p (the block containing e) must be written before * anything else. */ /* (i) */ memmove(e->score, b->score, VtScoreSize); vtentrypack(e, p->data, r->offset % r->epb); /* (ii) */ memmove(ob->data, vtzeroscore, VtScoreSize); /* (iii) */ vtblockput(rb); if(ob!=nil && ob!=rb) vtblockput(ob); vtblockput(b); if(DEPTH(e->type) == depth) return 0; return -1; }
VtFile* _vtfilecreate(VtFile *r, int o, int psize, int dsize, int type) { int i; VtBlock *b; u32int bn, size; VtEntry e; int epb; VtFile *rr; u32int offset; assert(ISLOCKED(r)); assert(psize <= VtMaxLumpSize); assert(dsize <= VtMaxLumpSize); assert(type == VtDirType || type == VtDataType); if(!r->dir){ werrstr(ENotDir); return nil; } epb = r->dsize/VtEntrySize; size = vtfilegetdirsize(r); /* * look at a random block to see if we can find an empty entry */ if(o == -1){ offset = lnrand(size+1); offset -= offset % epb; }else offset = o; /* try the given block and then try the last block */ for(;;){ bn = offset/epb; b = vtfileblock(r, bn, VtORDWR); if(b == nil) return nil; for(i=offset%r->epb; i<epb; i++){ if(vtentryunpack(&e, b->data, i) < 0) continue; if((e.flags&VtEntryActive) == 0 && e.gen != ~0) goto Found; } vtblockput(b); if(offset == size){ fprint(2, "vtfilecreate: cannot happen\n"); werrstr("vtfilecreate: cannot happen"); return nil; } offset = size; } Found: /* found an entry - gen already set */ e.psize = psize; e.dsize = dsize; e.flags = VtEntryActive; e.type = type; e.size = 0; memmove(e.score, vtzeroscore, VtScoreSize); vtentrypack(&e, b->data, i); offset = bn*epb + i; if(offset+1 > size){ if(vtfilesetdirsize(r, offset+1) < 0){ vtblockput(b); return nil; } } rr = vtfilealloc(r->c, b, r, offset, VtORDWR); vtblockput(b); return rr; }
static VtFile * vtfilealloc(VtCache *c, VtBlock *b, VtFile *p, u32int offset, int mode) { int epb; u32int size; VtEntry e; VtFile *r; assert(p==nil || ISLOCKED(p)); if(p == nil){ assert(offset == 0); epb = 1; }else epb = p->dsize / VtEntrySize; if(b->type != VtDirType){ werrstr("bad block type %#uo", b->type); return nil; } /* * a non-active entry is the only thing that * can legitimately happen here. all the others * get prints. */ if(vtentryunpack(&e, b->data, offset % epb) < 0){ fprint(2, "vtentryunpack failed: %r (%.*H)\n", VtEntrySize, b->data+VtEntrySize*(offset%epb)); return nil; } if(!(e.flags & VtEntryActive)){ werrstr("entry not active"); return nil; } if(DEPTH(e.type) < sizetodepth(e.size, e.psize, e.dsize)){ fprint(2, "depth %ud size %llud psize %ud dsize %ud\n", DEPTH(e.type), e.size, e.psize, e.dsize); werrstr("bad depth"); return nil; } size = vtcacheblocksize(c); if(e.dsize > size || e.psize > size){ werrstr("block sizes %ud, %ud bigger than cache block size %ud", e.psize, e.dsize, size); return nil; } r = vtmallocz(sizeof(VtFile)); r->c = c; r->mode = mode; r->dsize = e.dsize; r->psize = e.psize; r->gen = e.gen; r->dir = (e.type & VtTypeBaseMask) == VtDirType; r->ref = 1; r->parent = p; if(p){ qlock(&p->lk); assert(mode == VtOREAD || p->mode == VtORDWR); p->ref++; qunlock(&p->lk); }else{ assert(b->addr != NilBlock); r->local = 1; } memmove(r->score, b->score, VtScoreSize); r->offset = offset; r->epb = epb; return r; }