/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ #include "stdinc.h"#include "dat.h"#include "fns.h"#include "error.h"#include "9.h" static int sizeToDepth(uvlong s, int psize, int dsize);static u32int tagGen(void);static Block *sourceLoad(Source *r, Entry *e);static int sourceShrinkDepth(Source*, Block*, Entry*, int);static int sourceShrinkSize(Source*, Entry*, uvlong);static int sourceGrowDepth(Source*, Block*, Entry*, int); #define sourceIsLocked(r) ((r)->b != nil) static Source *sourceAlloc(Fs *fs, Block *b, Source *p, u32int offset, int mode, int issnapshot){ int epb; u32int epoch; char *pname = nil; Source *r; Entry e; assert(p==nil || sourceIsLocked(p)); if(p == nil){ assert(offset == 0); epb = 1; }else epb = p->dsize / VtEntrySize; if(b->l.type != BtDir) goto Bad; /* * a non-active entry is the only thing that * can legitimately happen here. all the others * get prints. */ if(!entryUnpack(&e, b->data, offset % epb)){ pname = sourceName(p); consPrint("%s: %s %V: sourceAlloc: entryUnpack failed\n", fs->name, pname, b->score); goto Bad; } if(!(e.flags & VtEntryActive)){ pname = sourceName(p); if(0) consPrint("%s: %s %V: sourceAlloc: not active\n", fs->name, pname, e.score); goto Bad; } if(e.psize < 256 || e.dsize < 256){ pname = sourceName(p); consPrint("%s: %s %V: sourceAlloc: psize %ud or dsize %ud < 256\n", fs->name, pname, e.score, e.psize, e.dsize); goto Bad; } if(e.depth < sizeToDepth(e.size, e.psize, e.dsize)){ pname = sourceName(p); consPrint("%s: %s %V: sourceAlloc: depth %ud size %llud " "psize %ud dsize %ud\n", fs->name, pname, e.score, e.depth, e.size, e.psize, e.dsize); goto Bad; } if((e.flags & VtEntryLocal) && e.tag == 0){ pname = sourceName(p); consPrint("%s: %s %V: sourceAlloc: flags %#ux tag %#ux\n", fs->name, pname, e.score, e.flags, e.tag); goto Bad; } if(e.dsize > fs->blockSize || e.psize > fs->blockSize){ pname = sourceName(p); consPrint("%s: %s %V: sourceAlloc: psize %ud or dsize %ud " "> blocksize %ud\n", fs->name, pname, e.score, e.psize, e.dsize, fs->blockSize); goto Bad; } epoch = b->l.epoch; if(mode == OReadWrite){ if(e.snap != 0){ vtSetError(ESnapRO); return nil; } }else if(e.snap != 0){ if(e.snap < fs->elo){ vtSetError(ESnapOld); return nil; } if(e.snap >= fs->ehi) goto Bad; epoch = e.snap; } r = vtMemAllocZ(sizeof(Source)); r->fs = fs; r->mode = mode; r->issnapshot = issnapshot; r->dsize = e.dsize; r->gen = e.gen; r->dir = (e.flags & VtEntryDir) != 0; r->lk = vtLockAlloc(); r->ref = 1; r->parent = p; if(p){ vtLock(p->lk); assert(mode == OReadOnly || p->mode == OReadWrite); p->ref++; vtUnlock(p->lk); } r->epoch = epoch;// consPrint("sourceAlloc: have %V be.%d fse.%d %s\n", b->score,// b->l.epoch, r->fs->ehi, mode == OReadWrite? "rw": "ro"); memmove(r->score, b->score, VtScoreSize); r->scoreEpoch = b->l.epoch; r->offset = offset; r->epb = epb; r->tag = b->l.tag; // consPrint("%s: sourceAlloc: %p -> %V %d\n", r, r->score, r->offset); return r;Bad: free(pname); vtSetError(EBadEntry); return nil;} Source *sourceRoot(Fs *fs, u32int addr, int mode){ Source *r; Block *b; b = cacheLocalData(fs->cache, addr, BtDir, RootTag, mode, 0); if(b == nil) return nil; if(mode == OReadWrite && b->l.epoch != fs->ehi){ consPrint("sourceRoot: fs->ehi = %ud, b->l = %L\n", fs->ehi, &b->l); blockPut(b); vtSetError(EBadRoot); return nil; } r = sourceAlloc(fs, b, nil, 0, mode, 0); blockPut(b); return r;} Source *sourceOpen(Source *r, ulong offset, int mode, int issnapshot){ ulong bn; Block *b; assert(sourceIsLocked(r)); if(r->mode == OReadWrite) assert(r->epoch == r->b->l.epoch); if(!r->dir){ vtSetError(ENotDir); return nil; } bn = offset/(r->dsize/VtEntrySize); b = sourceBlock(r, bn, mode); if(b == nil) return nil; r = sourceAlloc(r->fs, b, r, offset, mode, issnapshot); blockPut(b); return r;} Source *sourceCreate(Source *r, int dsize, int dir, u32int offset){ int i, epb, psize; u32int bn, size; Block *b; Entry e; Source *rr; assert(sourceIsLocked(r)); if(!r->dir){ vtSetError(ENotDir); return nil; } epb = r->dsize/VtEntrySize; psize = (dsize/VtScoreSize)*VtScoreSize; size = sourceGetDirSize(r); if(offset == 0){ /* * look at a random block to see if we can find an empty entry */ offset = lnrand(size+1); offset -= offset % epb; } /* try the given block and then try the last block */ for(;;){ bn = offset/epb; b = sourceBlock(r, bn, OReadWrite); if(b == nil) return nil; for(i=offset%r->epb; i<epb; i++){ entryUnpack(&e, b->data, i); if((e.flags&VtEntryActive) == 0 && e.gen != ~0) goto Found; } blockPut(b); if(offset == size){ fprint(2, "sourceCreate: cannot happen\n"); vtSetError("sourceCreate: cannot happen"); return nil; } offset = size; } Found: /* found an entry - gen already set */ e.psize = psize; e.dsize = dsize; assert(psize && dsize); e.flags = VtEntryActive; if(dir) e.flags |= VtEntryDir; e.depth = 0; e.size = 0; memmove(e.score, vtZeroScore, VtScoreSize); e.tag = 0; e.snap = 0; e.archive = 0; entryPack(&e, b->data, i); blockDirty(b); offset = bn*epb + i; if(offset+1 > size){ if(!sourceSetDirSize(r, offset+1)){ blockPut(b); return nil; } } rr = sourceAlloc(r->fs, b, r, offset, OReadWrite, 0); blockPut(b); return rr;} static intsourceKill(Source *r, int doremove){ Entry e; Block *b; u32int addr; u32int tag; int type; assert(sourceIsLocked(r)); b = sourceLoad(r, &e); if(b == nil) return 0; assert(b->l.epoch == r->fs->ehi); if(doremove==0 && e.size == 0){ /* already truncated */ blockPut(b); return 1; } /* remember info on link we are removing */ addr = globalToLocal(e.score); type = entryType(&e); tag = e.tag; if(doremove){ if(e.gen != ~0) e.gen++; e.dsize = 0; e.psize = 0; e.flags = 0; }else{ e.flags &= ~VtEntryLocal; } e.depth = 0; e.size = 0; e.tag = 0; memmove(e.score, vtZeroScore, VtScoreSize); entryPack(&e, b->data, r->offset % r->epb); blockDirty(b); if(addr != NilBlock) blockRemoveLink(b, addr, type, tag, 1); blockPut(b); if(doremove){ sourceUnlock(r); sourceClose(r); } return 1;} intsourceRemove(Source *r){ return sourceKill(r, 1);} intsourceTruncate(Source *r){ return sourceKill(r, 0);} uvlongsourceGetSize(Source *r){ Entry e; Block *b; assert(sourceIsLocked(r)); b = sourceLoad(r, &e); if(b == nil) return 0; blockPut(b); return e.size;} static intsourceShrinkSize(Source *r, Entry *e, uvlong size){ int i, type, ppb; uvlong ptrsz; u32int addr; uchar score[VtScoreSize]; Block *b; type = entryType(e); b = cacheGlobal(r->fs->cache, e->score, type, e->tag, OReadWrite); if(b == nil) return 0; ptrsz = e->dsize; ppb = e->psize/VtScoreSize; for(i=0; i+1<e->depth; i++) ptrsz *= ppb; while(type&BtLevelMask){ if(b->addr == NilBlock || b->l.epoch != r->fs->ehi){ /* not worth copying the block just so we can zero some of it */ blockPut(b); return 0; } /* * invariant: each pointer in the tree rooted at b accounts for ptrsz bytes */ /* zero the pointers to unnecessary blocks */ i = (size+ptrsz-1)/ptrsz; for(; i<ppb; i++){ addr = globalToLocal(b->data+i*VtScoreSize); memmove(b->data+i*VtScoreSize, vtZeroScore, VtScoreSize); blockDirty(b); if(addr != NilBlock) blockRemoveLink(b, addr, type-1, e->tag, 1); } /* recurse (go around again) on the partially necessary block */ i = size/ptrsz; size = size%ptrsz; if(size == 0){ blockPut(b); return 1; } ptrsz /= ppb; type--; memmove(score, b->data+i*VtScoreSize, VtScoreSize); blockPut(b); b = cacheGlobal(r->fs->cache, score, type, e->tag, OReadWrite); if(b == nil) return 0; } if(b->addr == NilBlock || b->l.epoch != r->fs->ehi){ blockPut(b); return 0; } /* * No one ever truncates BtDir blocks. */ if(type == BtData && e->dsize > size){ memset(b->data+size, 0, e->dsize-size); blockDirty(b); } blockPut(b); return 1;} intsourceSetSize(Source *r, uvlong size){ int depth; Entry e; Block *b; assert(sourceIsLocked(r)); if(size == 0) return sourceTruncate(r); if(size > VtMaxFileSize || size > ((uvlong)MaxBlock)*r->dsize){ vtSetError(ETooBig); return 0; } b = sourceLoad(r, &e); if(b == nil) return 0; /* quick out */ if(e.size == size){ blockPut(b); return 1; } depth = sizeToDepth(size, e.psize, e.dsize); if(depth < e.depth){ if(!sourceShrinkDepth(r, b, &e, depth)){ blockPut(b); return 0; } }else if(depth > e.depth){ if(!sourceGrowDepth(r, b, &e, depth)){ blockPut(b); return 0; } } if(size < e.size) sourceShrinkSize(r, &e, size); e.size = size; entryPack(&e, b->data, r->offset % r->epb); blockDirty(b); blockPut(b); return 1;} intsourceSetDirSize(Source *r, ulong ds){ uvlong size; int epb; assert(sourceIsLocked(r)); epb = r->dsize/VtEntrySize; size = (uvlong)r->dsize*(ds/epb); size += VtEntrySize*(ds%epb); return sourceSetSize(r, size);} ulongsourceGetDirSize(Source *r){ ulong ds; uvlong size; int epb; assert(sourceIsLocked(r)); epb = r->dsize/VtEntrySize; size = sourceGetSize(r); ds = epb*(size/r->dsize); ds += (size%r->dsize)/VtEntrySize; return ds;} Lines 1–500 of 1077. Load more
/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ #include "stdinc.h" #include "dat.h" #include "fns.h" #include "error.h" #include "9.h" static int sizeToDepth(uvlong s, int psize, int dsize); static u32int tagGen(void); static Block *sourceLoad(Source *r, Entry *e); static int sourceShrinkDepth(Source*, Block*, Entry*, int); static int sourceShrinkSize(Source*, Entry*, uvlong); static int sourceGrowDepth(Source*, Block*, Entry*, int); #define sourceIsLocked(r) ((r)->b != nil) static Source * sourceAlloc(Fs *fs, Block *b, Source *p, u32int offset, int mode, int issnapshot) { int epb; u32int epoch; char *pname = nil; Source *r; Entry e; assert(p==nil || sourceIsLocked(p)); if(p == nil){ assert(offset == 0); epb = 1; }else epb = p->dsize / VtEntrySize; if(b->l.type != BtDir) goto Bad; /* * a non-active entry is the only thing that * can legitimately happen here. all the others * get prints. */ if(!entryUnpack(&e, b->data, offset % epb)){ pname = sourceName(p); consPrint("%s: %s %V: sourceAlloc: entryUnpack failed\n", fs->name, pname, b->score); goto Bad; } if(!(e.flags & VtEntryActive)){ pname = sourceName(p); if(0) consPrint("%s: %s %V: sourceAlloc: not active\n", fs->name, pname, e.score); goto Bad; } if(e.psize < 256 || e.dsize < 256){ pname = sourceName(p); consPrint("%s: %s %V: sourceAlloc: psize %ud or dsize %ud < 256\n", fs->name, pname, e.score, e.psize, e.dsize); goto Bad; } if(e.depth < sizeToDepth(e.size, e.psize, e.dsize)){ pname = sourceName(p); consPrint("%s: %s %V: sourceAlloc: depth %ud size %llud " "psize %ud dsize %ud\n", fs->name, pname, e.score, e.depth, e.size, e.psize, e.dsize); goto Bad; } if((e.flags & VtEntryLocal) && e.tag == 0){ pname = sourceName(p); consPrint("%s: %s %V: sourceAlloc: flags %#ux tag %#ux\n", fs->name, pname, e.score, e.flags, e.tag); goto Bad; } if(e.dsize > fs->blockSize || e.psize > fs->blockSize){ pname = sourceName(p); consPrint("%s: %s %V: sourceAlloc: psize %ud or dsize %ud " "> blocksize %ud\n", fs->name, pname, e.score, e.psize, e.dsize, fs->blockSize); goto Bad; } epoch = b->l.epoch; if(mode == OReadWrite){ if(e.snap != 0){ vtSetError(ESnapRO); return nil; } }else if(e.snap != 0){ if(e.snap < fs->elo){ vtSetError(ESnapOld); return nil; } if(e.snap >= fs->ehi) goto Bad; epoch = e.snap; } r = vtMemAllocZ(sizeof(Source)); r->fs = fs; r->mode = mode; r->issnapshot = issnapshot; r->dsize = e.dsize; r->gen = e.gen; r->dir = (e.flags & VtEntryDir) != 0; r->lk = vtLockAlloc(); r->ref = 1; r->parent = p; if(p){ vtLock(p->lk); assert(mode == OReadOnly || p->mode == OReadWrite); p->ref++; vtUnlock(p->lk); } r->epoch = epoch; // consPrint("sourceAlloc: have %V be.%d fse.%d %s\n", b->score, // b->l.epoch, r->fs->ehi, mode == OReadWrite? "rw": "ro"); memmove(r->score, b->score, VtScoreSize); r->scoreEpoch = b->l.epoch; r->offset = offset; r->epb = epb; r->tag = b->l.tag; // consPrint("%s: sourceAlloc: %p -> %V %d\n", r, r->score, r->offset); return r; Bad: free(pname); vtSetError(EBadEntry); return nil; } Source * sourceRoot(Fs *fs, u32int addr, int mode) { Source *r; Block *b; b = cacheLocalData(fs->cache, addr, BtDir, RootTag, mode, 0); if(b == nil) return nil; if(mode == OReadWrite && b->l.epoch != fs->ehi){ consPrint("sourceRoot: fs->ehi = %ud, b->l = %L\n", fs->ehi, &b->l); blockPut(b); vtSetError(EBadRoot); return nil; } r = sourceAlloc(fs, b, nil, 0, mode, 0); blockPut(b); return r; } Source * sourceOpen(Source *r, ulong offset, int mode, int issnapshot) { ulong bn; Block *b; assert(sourceIsLocked(r)); if(r->mode == OReadWrite) assert(r->epoch == r->b->l.epoch); if(!r->dir){ vtSetError(ENotDir); return nil; } bn = offset/(r->dsize/VtEntrySize); b = sourceBlock(r, bn, mode); if(b == nil) return nil; r = sourceAlloc(r->fs, b, r, offset, mode, issnapshot); blockPut(b); return r; } Source * sourceCreate(Source *r, int dsize, int dir, u32int offset) { int i, epb, psize; u32int bn, size; Block *b; Entry e; Source *rr; assert(sourceIsLocked(r)); if(!r->dir){ vtSetError(ENotDir); return nil; } epb = r->dsize/VtEntrySize; psize = (dsize/VtScoreSize)*VtScoreSize; size = sourceGetDirSize(r); if(offset == 0){ /* * look at a random block to see if we can find an empty entry */ offset = lnrand(size+1); offset -= offset % epb; } /* try the given block and then try the last block */ for(;;){ bn = offset/epb; b = sourceBlock(r, bn, OReadWrite); if(b == nil) return nil; for(i=offset%r->epb; i<epb; i++){ entryUnpack(&e, b->data, i); if((e.flags&VtEntryActive) == 0 && e.gen != ~0) goto Found; } blockPut(b); if(offset == size){ fprint(2, "sourceCreate: cannot happen\n"); vtSetError("sourceCreate: cannot happen"); return nil; } offset = size; } Found: /* found an entry - gen already set */ e.psize = psize; e.dsize = dsize; assert(psize && dsize); e.flags = VtEntryActive; if(dir) e.flags |= VtEntryDir; e.depth = 0; e.size = 0; memmove(e.score, vtZeroScore, VtScoreSize); e.tag = 0; e.snap = 0; e.archive = 0; entryPack(&e, b->data, i); blockDirty(b); offset = bn*epb + i; if(offset+1 > size){ if(!sourceSetDirSize(r, offset+1)){ blockPut(b); return nil; } } rr = sourceAlloc(r->fs, b, r, offset, OReadWrite, 0); blockPut(b); return rr; } static int sourceKill(Source *r, int doremove) { Entry e; Block *b; u32int addr; u32int tag; int type; assert(sourceIsLocked(r)); b = sourceLoad(r, &e); if(b == nil) return 0; assert(b->l.epoch == r->fs->ehi); if(doremove==0 && e.size == 0){ /* already truncated */ blockPut(b); return 1; } /* remember info on link we are removing */ addr = globalToLocal(e.score); type = entryType(&e); tag = e.tag; if(doremove){ if(e.gen != ~0) e.gen++; e.dsize = 0; e.psize = 0; e.flags = 0; }else{ e.flags &= ~VtEntryLocal; } e.depth = 0; e.size = 0; e.tag = 0; memmove(e.score, vtZeroScore, VtScoreSize); entryPack(&e, b->data, r->offset % r->epb); blockDirty(b); if(addr != NilBlock) blockRemoveLink(b, addr, type, tag, 1); blockPut(b); if(doremove){ sourceUnlock(r); sourceClose(r); } return 1; } int sourceRemove(Source *r) { return sourceKill(r, 1); } int sourceTruncate(Source *r) { return sourceKill(r, 0); } uvlong sourceGetSize(Source *r) { Entry e; Block *b; assert(sourceIsLocked(r)); b = sourceLoad(r, &e); if(b == nil) return 0; blockPut(b); return e.size; } static int sourceShrinkSize(Source *r, Entry *e, uvlong size) { int i, type, ppb; uvlong ptrsz; u32int addr; uchar score[VtScoreSize]; Block *b; type = entryType(e); b = cacheGlobal(r->fs->cache, e->score, type, e->tag, OReadWrite); if(b == nil) return 0; ptrsz = e->dsize; ppb = e->psize/VtScoreSize; for(i=0; i+1<e->depth; i++) ptrsz *= ppb; while(type&BtLevelMask){ if(b->addr == NilBlock || b->l.epoch != r->fs->ehi){ /* not worth copying the block just so we can zero some of it */ blockPut(b); return 0; } /* * invariant: each pointer in the tree rooted at b accounts for ptrsz bytes */ /* zero the pointers to unnecessary blocks */ i = (size+ptrsz-1)/ptrsz; for(; i<ppb; i++){ addr = globalToLocal(b->data+i*VtScoreSize); memmove(b->data+i*VtScoreSize, vtZeroScore, VtScoreSize); blockDirty(b); if(addr != NilBlock) blockRemoveLink(b, addr, type-1, e->tag, 1); } /* recurse (go around again) on the partially necessary block */ i = size/ptrsz; size = size%ptrsz; if(size == 0){ blockPut(b); return 1; } ptrsz /= ppb; type--; memmove(score, b->data+i*VtScoreSize, VtScoreSize); blockPut(b); b = cacheGlobal(r->fs->cache, score, type, e->tag, OReadWrite); if(b == nil) return 0; } if(b->addr == NilBlock || b->l.epoch != r->fs->ehi){ blockPut(b); return 0; } /* * No one ever truncates BtDir blocks. */ if(type == BtData && e->dsize > size){ memset(b->data+size, 0, e->dsize-size); blockDirty(b); } blockPut(b); return 1; } int sourceSetSize(Source *r, uvlong size) { int depth; Entry e; Block *b; assert(sourceIsLocked(r)); if(size == 0) return sourceTruncate(r); if(size > VtMaxFileSize || size > ((uvlong)MaxBlock)*r->dsize){ vtSetError(ETooBig); return 0; } b = sourceLoad(r, &e); if(b == nil) return 0; /* quick out */ if(e.size == size){ blockPut(b); return 1; } depth = sizeToDepth(size, e.psize, e.dsize); if(depth < e.depth){ if(!sourceShrinkDepth(r, b, &e, depth)){ blockPut(b); return 0; } }else if(depth > e.depth){ if(!sourceGrowDepth(r, b, &e, depth)){ blockPut(b); return 0; } } if(size < e.size) sourceShrinkSize(r, &e, size); e.size = size; entryPack(&e, b->data, r->offset % r->epb); blockDirty(b); blockPut(b); return 1; } int sourceSetDirSize(Source *r, ulong ds) { uvlong size; int epb; assert(sourceIsLocked(r)); epb = r->dsize/VtEntrySize; size = (uvlong)r->dsize*(ds/epb); size += VtEntrySize*(ds%epb); return sourceSetSize(r, size); } ulong sourceGetDirSize(Source *r) { ulong ds; uvlong size; int epb; assert(sourceIsLocked(r)); epb = r->dsize/VtEntrySize; size = sourceGetSize(r); ds = epb*(size/r->dsize); ds += (size%r->dsize)/VtEntrySize; return ds; }