ref: b3e875d7db45fca0cde9f4efa12872f61afc54d7
dir: /iobuf.c/
#include "all.h" u64 nbuckets = 0; /* number of hash buckets, -m changes it */ Hiob *hiob = nil; /* array of nbuckets */ Extents frees = {0};/* extents of free blocks on the disk */ /* extents of Blocksize units of memory used to store the disk block contents in memory for the buffer cache and write queue */ Extents memunits = {0}; u8 *memunitpool = nil; u8 *memunitstart = nil; /* using nunits + 1 for alignment */ void initmemunitpool(u64 nunits) { memunitstart = sbrk((nunits+1) * Blocksize); memunitpool = memunitstart+Blocksize- ((u64)memunitstart%Blocksize); initextents(&memunits, "memunits", 1, 0, 2, fsflush, dprintfd, panic, malloc9p); if(chatty9p > 4) dprint("initmemunitpool: memunitpool %p nunits*Blocksize %p\n", memunitpool, nunits*Blocksize); if(nunits > 0) ufree(&memunits, 0, nunits); else panic("invalid nunits %llud\n", nunits); } u8 * allocmemunits(u16 len) { u64 m; u8 *a; ualloc(&memunits, len, &m); if(chatty9p > 4) dprint("allocmemunit: memunitpool %p m %p\n", memunitpool, m); a = memunitpool+(m*Blocksize); memset(a, 0, len*Blocksize); return a; } void freememunits(u8 *m, u16 len) { if(m == 0) panic("freememunit: m == 0\n"); if((m-memunitpool)%Blocksize) panic("freememunit: (m-memunitpool)%%Blocksize %llud\n", (u64)(m-memunitpool)%Blocksize); ufree(&memunits, (m-memunitpool)/Blocksize, len); } u32 showhashbucket(Iobuf *p, s8 *buf, u32 nbuf) { u32 n; char locked[32]; u8 tag; // char *name; // u16 namelen; if(p == nil) return 0; if(canwlock(p)){ strcpy(locked, "unlocked"); wunlock(p); }else strcpy(locked, "locked"); if(p->tag == Tdata) n = snprint(buf, nbuf, "%s %llud %s\n", tagnames[p->tag], p->blkno, locked); else{ tag = p->d->tag; if(tag == Tdentry && p->blkno == Bdroot){ n = snprint(buf, nbuf, "%s %llud / %lluds %s %s %llud\n", tagnames[tag], p->blkno, (nsec()-p->atime)/Nsec, locked, p->append!=nil?"data":"nil", p->appendsize); }else if(tag == Tdentry){ /* cannot do this here -- It causes a deadlock pdbuf = egetmetachk(p->d->pdblkno, Breadonly, Tdentry, p->d->pqpath); if(waserror()){ putbuf(pdbuf, 0); nexterror(); } pd = pdbuf->d; readname(pd, p->d->preli, &name, &namelen); n = snprint(buf, nbuf, "%s %llud %s %lluds %s %s %llud\n", tagnames[tag], p->blkno, name, (nsec()-p->atime)/Nsec, locked, p->append!=nil?"data":"nil", p->appendsize); poperror(); putbuf(pdbuf, 0); free(name); */ n = snprint(buf, nbuf, "%s %llud %lluds %s %s %llud\n", tagnames[tag], p->blkno, (nsec()-p->atime)/Nsec, locked, p->append!=nil?"data":"nil", p->appendsize); }else n = snprint(buf, nbuf, "%s %llud %lluds %s\n", tagnames[tag], p->blkno, (nsec()-p->atime)/Nsec, locked); } return n; } u32 showhashbuckets(s8 *buf, u32 nbuf) { Iobuf *p; Hiob *hp; u32 n; u64 i; for(n = 0, i = 0; i < nbuckets; i++){ hp=&hiob[i]; qlock(hp); if(waserror()){ qunlock(hp); nexterror(); } if(hp->link != nil){ n += snprint(buf+n, nbuf-n, "i %llud ", i); n += showhashbucket(hp->link, buf+n, nbuf-n); for(p = hp->link->fore; p!=hp->link; p=p->fore){ n+=showhashbucket(p, buf+n, nbuf-n); } // n+=snprint(buf+n,nbuf-n,"\n"); } poperror(); qunlock(hp); } return n; } /* flushold: flush old unflushed data appended to the Tdentry to the disk remove old Iobuf's until hp->n <= Ncollisions sync: send all Tdata to the disk */ void flushold(void) { Iobuf *p; Hiob *hp; u64 i; // char buf[1024]={'\0'}; /* flush old unflushed data appended to the Tdentry */ /* dprint("flushold start\n"); showhashbuckets(buf, 1024); dprint(buf);*/ Again: for(i = 0; i < nbuckets; i++){ hp=&hiob[i]; qlock(hp); if(hp->link != nil){ for(p = hp->link->back; p!=hp->link; p=p->back){ /* all new data from here */ if(p->atime > nsec()-Nrefresh) break; if(p->ref == 0 && p->tag == Tdentry && p->append != nil && canwlock(p)){ p->callerpc = 1; qunlock(hp); flush(p); goto Again; } } if(p == hp->link && p->ref == 0 && p->atime < nsec()-Nrefresh && p->tag == Tdentry && p->append != nil && canwlock(p)){ qunlock(hp); flush(p); goto Again; } } qunlock(hp); } Oldbufs: /* remove old Iobuf's until hp->n <= Ncollisions */ for(i = 0; i < nbuckets; i++){ hp=&hiob[i]; qlock(hp); if(hp->n > Ncollisions){ if(hp->link != nil){ for(p = hp->link->back; hp->n > Ncollisions && p!=hp->link; p=p->back){ /* all new data from here */ if(p->atime > nsec()-Nrefresh) break; if(p->ref == 0 && canwlock(p)){ incref(p); /* not needed */ /* remove p from its current position in the lru circular buffer */ p->back->fore = p->fore; p->fore->back = p->back; hp->n--; p->callerpc = 3; qunlock(hp); if(p->tag == Tdentry) flush(p); freememunits(p->xiobuf, p->len); free(p); goto Oldbufs; } } } } qunlock(hp); } } u64 sync(void) { Iobuf *p; Hiob *hp; u64 nlocked, i; /* flush any unflushed data appended to the Tdentry */ Resync: nlocked = 0; for(i = 0; i < nbuckets; i++){ hp=&hiob[i]; qlock(hp); if(hp->link != nil){ for(p = hp->link->back; p!=hp->link; p=p->back){ if(p->tag == Tdentry && p->append != nil){ if(p->ref == 0 && canwlock(p)){ qunlock(hp); flush(p); goto Resync; }else nlocked++; } } if(p == hp->link && p->tag == Tdentry){ if(p->ref == 0 && p->append != nil && canwlock(p)){ qunlock(hp); flush(p); goto Resync; } } } qunlock(hp); } return nlocked; } /* Get the Iobuf of the disk block at addr from the buffer cache for my use. All disk accesses go through the buffer cache. getbuf() selects the Iobuf for our use from the buffer cache. putbuf() returns the Iobuf back to the buffer cache. Any Iobuf access happens only between the getbuf() and putbuf() calls. The Iobuf's are grouped into a least-recently-used circular list of buffers. The most recently used Iobuf is pointed to by Hiob.link. Iobuf.fore is the next recently used buffer. Iobuf.back is the oldest recently used buffer. Hiob.link->back is the oldest buffer that will be reused first. */ Iobuf * getbuf(u64 blkno, u16 len, u8 readonly, u8 freshalloc, u64 callerpc) { Hiob *hp; Iobuf *s, *p; if(len > Maxdatablockunits) panic("getbuf(): invalid len %ud blkno %llud\n", len, blkno); hp = &hiob[blkno%nbuckets]; if(chatty9p > 4) dprint("getbuf blkno %llud blkno%%nbuckets %llud pc 0x%p" " hiob 0x%p hp 0x%p readonly %d\n", blkno, blkno%nbuckets, getcallerpc(&blkno), hiob, hp, readonly); qlock(hp); s = hp->link; if(s == nil) goto new; for(p=s;;){ if(p->blkno == blkno){ if(p != s){ /* remove p from its current position in the lru circular buffer */ p->back->fore = p->fore; p->fore->back = p->back; /* make p the hb->link and put it at the back of existing link */ p->fore = s; p->back = s->back; s->back = p; p->back->fore = p; hp->link = p; } incref(p); qunlock(hp); if(chatty9p > 4) dprint(" in cache, after qunlock(hp) hp 0x%p blkno %llud\n", hp, blkno); if(p->len != len){ wlock(p); /* has someone done this change already? */ if(p->len != len){ freememunits(p->xiobuf, p->len); p->xiobuf = allocmemunits(len); p->len = len; p->freshalloc = freshalloc; if(freshalloc == 0) devread(blkno, p->xiobuf, len); if(readonly){ if(chkwunlock(p) == 0){ showbuf(p); panic("getbuf chkwunlock(p) == 0 called by %#p\n", getcallerpc(&blkno)); } rlock(p); } } }else if(readonly){ if(chatty9p > 4) dprint(" in cache iobuf 0x%p has len %llud blkno %llud len %llud .." " rlock()\n", p, p->len, blkno, len); rlock(p); }else{ wlock(p); if(chatty9p > 4) dprint(" after wlock() blkno %llud\n", blkno); } p->callerpc = callerpc; decref(p); return p; } p = p->fore; if(p == s) break; } /* maxed out our allowed number of collisions, try to steal an older Iobuf without any ref's and not in the write queue. Ncollisions is a soft limit. We are not moving the stolen buffer to the top of the circular linked list, but, setting this stolen buffer as the lru. I figure it should not matter much either way. If it does, there is a changelru() function to do so in the git history that can be reused. incref(Iobuf) only happens with a qlock(hash bucket). */ if(hp->n >= Ncollisions){ for(p = hp->link->back; p != hp->link; p = p->back){ if(p->ref == 0 && p->append != nil){ if(canwlock(p)){ /* p->ref cannot change without a lock on the hash bucket */ if(p->append != nil){ wunlock(p); continue; } incref(p); goto found; /* p is wlock() */ } } } } /* no unlocked blocks available; add a new one */ new: if(chatty9p > 4) dprint(" adding new Iobuf for blkno %llud\n", blkno); p = emalloc9p(sizeof(Iobuf)); wlock(p); incref(p); hp->n++; found: p->blkno = blkno; s = hp->link; if(s == nil){ /* for new Iobuf */ p->fore = p; p->back = p; }else{ /* for stolen Iobuf */ if(p->fore != nil && p->back != nil){ /* remove p from its current position in the lru circular buffer */ p->back->fore = p->fore; p->fore->back = p->back; } /* make p the hb->link and put it at the back of existing link */ p->fore = s; p->back = s->back; s->back = p; p->back->fore = p; } hp->link = p; qunlock(hp); if(chatty9p > 4) dprint(" after qunlock(hp) hp 0x%p blkno %llud p 0x%p p->fore 0x%p hp->link 0x%p\n", hp, blkno, p, p->fore, hp->link); if(p->len != len){ if(p->len > 0) freememunits(p->xiobuf, p->len); p->xiobuf = allocmemunits(len); p->len = len; }else memset(p->xiobuf, 0, p->len*Blocksize); p->freshalloc = freshalloc; if(freshalloc == 0) devread(blkno, p->xiobuf, len); if(readonly){ wunlock(p); rlock(p); } p->callerpc = callerpc; decref(p); return p; } Iobuf * getbufchk(u64 blkno, u16 len, u8 readonly, int tag, u64 qpath, u64 userpc) { Iobuf *b; b = getbuf(blkno, len, readonly, Bused, userpc); if(b != nil){ b->tag = b->xiobuf[0]; if(checktag(b, len, tag, qpath) == 0){ putbuf(b, 0); panic("checktag on %llud failed %s\n", blkno, errstring[Ephase]); } } if(b->io == nil) panic("b->io == nil blkno %llud readonly %d tag %d" " qpath %llud b->blkno %llud caller %#p\n", blkno, readonly, tag, qpath, b->blkno, getcallerpc(&blkno)); return b; } Iobuf * egetbufchk(u64 blkno, u16 len, u8 readonly, int tag, u64 qpath, u64 userpc) { Iobuf *b; b = getbuf(blkno, len, readonly, Bused, userpc); if(b == nil){ error(errstring[Ephase]); }else{ if(waserror()){ putbuf(b, 0); nexterror(); } b->tag = b->xiobuf[0]; checktag(b, len, tag, qpath); poperror(); } if(b->io == nil) panic("b->io == nil blkno %llud readonly %d tag %d" " qpath %llud b->blkno %llud caller %#p\n", blkno, readonly, tag, qpath, b->blkno, getcallerpc(&blkno)); return b; } Iobuf * egetmetachk(u64 blkno, u8 readonly, int tag, u64 qpath) { return egetbufchk(blkno, 1, readonly, tag, qpath, getcallerpc(&blkno)); } Iobuf * egetmeta(u64 blkno, u8 readonly, u8 freshalloc) { Iobuf *b; b = getbuf(blkno, 1, readonly, freshalloc, getcallerpc(&blkno)); if(b == nil) error(errstring[Ephase]); else b->tag = b->xiobuf[0]; return b; } Iobuf * getmeta(u64 blkno, u8 readonly, u8 freshalloc) { Iobuf *b; b = getbuf(blkno, 1, readonly, freshalloc, getcallerpc(&blkno)); if(b == nil) panic("getmeta blkno %llud failed\n", blkno); else b->tag = b->xiobuf[0]; return b; } /* put the Iobuf of the disk block at addr back into the buffer cache for others to use. writes to disk if changed. if(chatty9p > 4) dprint("putbuf p->blkno 0x%d t->c->type %d devtab[t->c->type]->dc %c\n" " p 0x%p p->readonly %d\n" " p->xiobuf 0x%p", p->blkno, t->c->type, devtab[t->c->type]->dc, p, p->readonly, p->xiobuf); */ void bkp(u64 srcbno, u8 *contents, u64 bno, u64 qpath) { Iobuf *buf; if(bno == 0){ dprint("bkp %llud: invalid backup location %llud, qpath %llud\n", srcbno, bno, qpath); return; } buf = egetmetachk(bno, Bwritable, Tdentry, qpath); memcpy(buf->d->buf, contents, Ddatasize); buf->d->mtime = nsec(); // if(qpath == Qproot0 || qpath == Qproot1){ // buf->d->mode &= ~DMDIR; /* to avoid recursive du -a */ // } putbuf(buf, 1); } void putbuf(Iobuf *p, u8 dowrite) { u8 buf[Ddatasize]; u64 srcbno; if(p == nil){ panic("putbuf p == nil called by %#p\n", getcallerpc(&p)); dprint("%s\n", errstring[Ephase]); return; } if(p->io == nil){ showbuf(p); panic("putbuf p->io == nil by %#p\n", getcallerpc(&p)); dprint("%s\n", errstring[Ephase]); return; } if(chatty9p > 4) dprint("putbuf p->blkno %llud\n", p->blkno); p->atime = nsec(); if(p->readers){ chkrunlock(p); if(chatty9p > 4) dprint(" .. runlock()'ed\n"); }else{ srcbno = p->blkno; if(dowrite){ devwrite(p->blkno, p->xiobuf, p->len); if(p->blkno == config.config.srcbno || p->blkno == config.super.srcbno || p->blkno == config.root.srcbno) memcpy(buf, p->d->buf, Ddatasize); } if(chkwunlock(p) == 0){ showbuf(p); panic("putbuf: chkwunlock(p) == 0 called by %#p\n", getcallerpc(&p)); } if(dowrite){ if(srcbno == config.config.srcbno){ bkp(srcbno, buf, config.config.dest[0], Qpconfig0); }else if(srcbno == config.super.srcbno){ bkp(srcbno, buf, config.super.dest[0], Qpsuper0); }else if(srcbno == config.root.srcbno){ bkp(srcbno, buf, config.root.dest[0], Qproot0); } } } } int checktag(Iobuf *p, u16 len, u8 tag, u64 qpath) { uintptr pc; u8 buftag; u64 bufqpath; buftag = p->xiobuf[0]; bufqpath = p->xiobuf64[p->len*Nu64perblock -1]; if(tag == Tdata && len != p->io->len){ pc = getcallerpc(&p); dprint("checktag Tdata len failed: p->blkno %llud:" " actual p->len %hud p->io->len %hud tag/path %G/%llud;" " expected len %hud tag/path %G/%llud\n", p->blkno, p->len, p->io->len, buftag, bufqpath, len, tag, qpath); dprint("checktag Tdata len failed: pc=%p disk %s(block %llud)" " p->len %hud p->io->len %hud tag/path=%s/%llud;" " expected len %hud tag/path=%s/%llud\n", pc, devfile, p->blkno, p->len, p->io->len, tagnames[buftag], bufqpath, len, tagnames[tag], qpath); panic("checktag failed\n"); error(errstring[Ephase]); // use a better error message } if(len != p->len || tag != buftag || qpath != bufqpath){ pc = getcallerpc(&p); dprint("checktag p->blkno %llud:" " actual len %hud tag/path %G/%llud;" " expected len %hud tag/path %G/%llud\n", p->blkno, p->len, buftag, bufqpath, len, tag, qpath); dprint("checktag pc=%p disk %s(block %llud)" " len %hud tag/path=%s/%llud;" " expected len %hud tag/path=%s/%llud\n", pc, devfile, p->blkno, p->len, tagnames[buftag], bufqpath, len, tagnames[tag], qpath); panic("checktag failed\n"); error(errstring[Ephase]); return 0; } return 1; } void settag(Iobuf *p, u8 tag, u64 qpath) { if(p->readers) panic("settag %s(%llux) tag/path=%s/%llud: not Bwritable\n", devfile, (u64)p->blkno, tagnames[tag], qpath); if(p->io == nil) panic("settag %s(%llux) tag/path=%s/%llud: p->io == nil\n", devfile, (u64)p->blkno, tagnames[tag], qpath); p->tag = p->xiobuf[0] = tag; p->xiobuf64[p->len*Nu64perblock -1] = qpath; } void *amalloc(u64 n){ void *p; if(p = mallocalign(n, sizeof(u64), 0, 0)) memset(p, 0, n); else sysfatal("malloc: %r"); setmalloctag(p, getcallerpc(&n)); return p; } /* * Prepare nbuckets of hash buckets. Each bucket will point to a * linked list of collisions. The collisions are ordered into a * least-recently-used (lru) linked list. */ void iobufinit(void) { int i; Hiob *hp; while(prime(nbuckets) == 0) nbuckets++; if(chatty9p) dprint("mafs: iobufinit %ud hash buckets\n", nbuckets); hiob = amalloc(nbuckets*sizeof(Hiob)); hp = hiob; if(chatty9p > 4) dprint("iobufinit: hiob 0x%p\n", hiob); for(i=0; i<nbuckets; i++){ qlock(hp); qunlock(hp); hp++; } } void showbuf(Iobuf *p) { if(p == nil){ dprint("showbuf p == nil called by %#p\n", p, getcallerpc(&p)); return; } dprint("showbuf p 0x%p %s ref %d readers %d writer %d" " blkno %llud len %d" " fore 0x%p back 0x%p" " xiobuf 0x%p" " caller %#p\n", p, tagnames[p->tag], p->ref, p->readers, p->writer, p->blkno, p->len, p->fore, p->back, p->xiobuf, getcallerpc(&p)); if(p->io != nil) showblock(2, (u8*)p->io); }