/*
* read the inode blocks and make sure they
* haven't been trashed.
*
* make the in-core table of qid to inode mappings.
* N.B. this is just an array. we need a linear search to find
* a particular inode. this could be done faster.
*
* nab is the first inode block.
*/
int
iinit(Icache *ic, int f, int psize, char* name)
{
Ibuf *b;
Imap *m;
ulong ino;
Bbuf *bb;
Dinode *bi;
/*
* get basic sizes and allocation info from disk
*/
if(dinit(ic, f, psize, name) < 0)
return -1;
/*
* read first inode block to get number of inodes
*/
bb = bcread(ic, ic->nab);
if(bb == 0){
fprint(2, "iinit: can't read disk\n");
return -1;
}
bi = (Dinode*)bb->data;
if(bi->nino==0 || bi->nino>2048){
fprint(2, "iinit: bad nino\n");
return -1;
}
ic->nino = bi->nino;
/*
* set up sizing constants
*/
ic->i2b = (ic->bsize - sizeof(Dihdr))/sizeof(Inode);
ic->nib = (ic->nino + ic->i2b - 1)/ic->i2b;
/*
* mark all cache buffers as empty, put them on the lru list
*/
lruinit(&ic->blru);
for(b = ic->ib; b < &ic->ib[Nicache]; b++){
b->inuse = 0;
lruadd(&ic->blru, b);
}
/*
* format the inode blocks
*/
int
iformat(Icache *ic, int f, ulong nino, char *name, int bsize, int psize)
{
int nib;
ulong bno, i2b, i;
Bbuf *bb;
Dinode *bi;
/*
* first format disk allocation
*/
if(dformat(ic, f, name, bsize, psize) < 0)
return -1;
/*
* get an inode into the cache. if no inode exists for this qid, create one
* from an unused qid/inode map.
*/
Ibuf *
iget(Icache *ic, Qid qid)
{
Imap *m, *me;
Ibuf *b;
/*
* find map entry with same qid.path
*/
for(m = ic->map, me = &ic->map[ic->nino]; m < me; m++)
if(m->inuse && m->qid.path==qid.path){
if(m->qid.vers != qid.vers){
/*
* our info is old, forget it
*/
DPRINT(2, "updating old file %llud.%lud\n",
qid.path, qid.vers);
m->qid = qid;
iupdate(ic, m - ic->map, qid);
}
break;
}
/*
* if an already existing inode, just get it
*/
if(m != me)
return iread(ic, m - ic->map);
/*
* create a new inode, throw out the least recently used inode
* if necessary
*/
m = (Imap*)ic->mlru.lnext;
if(m->inuse){
DPRINT(2, "superceding file %llud.%ld by %llud.%ld\n",
m->qid.path, m->qid.vers, qid.path, qid.vers);
if(iremove(ic, m - ic->map) < 0)
return 0;
}
if(statson)
cfsstat.ninsert++;
/*
* init inode and write to disk
*/
DPRINT(2, "new file %llud.%ld ino %zd\n",
qid.path, qid.vers, m - ic->map);
b = ialloc(ic, m - ic->map);
b->inode.inuse = m->inuse = 1;
b->inode.qid = qid;
b->inode.length = 0x7fffffffffffffffLL;
m->qid = qid;
b->inode.ptr.bno = Notabno;
iwrite(ic, b);
return b;
}
/*
* read an inode into the cache
*
* ASSUMPTION: the inode is valid
*/
Ibuf*
iread(Icache *ic, ulong ino)
{
Ibuf *b;
Imap *m;
ulong bno;
Bbuf *bb;
Dinode *bi;
/*
* first see if we already have it in a cache entry
*/
m = &ic->map[ino];
if(m->inuse && m->b){
b = m->b;
goto out;
}
/*
* Forget what we know about an inode without removing it
*
* N.B: ordering of iwrite and dfree is important
*/
int
iupdate(Icache *ic, ulong ino, Qid qid)
{
Ibuf *b;
Imap *m;
Dptr d;
/*
* update inode and map
*/
b->inode.qid = qid;
b->inode.length = 0x7fffffffffffffffLL; /* Set to maximum */
m = &ic->map[ino];
m->qid = qid;
/*
* the free is not done if the write fails!
* this is important
*/
d = b->inode.ptr;
b->inode.ptr.bno = Notabno;
if(iwrite(ic, b) < 0)
return -1;
dfree(ic, &d);
return 0;
}
/*
* remove an inode
*
* N.B: ordering of iwrite and dfree is important
*/
int
iremove(Icache *ic, ulong ino)
{
Ibuf *b;
Imap *m;
if(statson)
cfsstat.ndelete++;
m = &ic->map[ino];
/*
* read in inode
*/
b = iread(ic, ino);
if(b == 0)
return -1;
/*
* mark it unused on disk
*/
b->inode.inuse = 0;
if(iwrite(ic, b) < 0)
return -1;
/*
* throw out it's data pages
*/
dfree(ic, &b->inode.ptr);
/*
* free the inode buffer
*/
ifree(ic, b);
/*
* make map entry least recently used
*/
lruderef(&ic->mlru, m);
return 0;
}
/*
* increment our version number
*/
void
iinc(Icache *ic, Ibuf *b)
{
b->inode.qid.vers++;
ic->map[b->ino].qid = b->inode.qid;
iwrite(ic, b);
}