shithub: gefs

ref: 7fa15946915dfffc3856c9075b20f591d1b00195
dir: /cache.c/

View raw version
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <avl.h>

#include "dat.h"
#include "fns.h"

static void
lrudel(Blk *b)
{
	if(b == fs->chead)
		fs->chead = b->cnext;
	if(b == fs->ctail)
		fs->ctail = b->cprev;
	if(b->cnext != nil)
		b->cnext->cprev = b->cprev;
	if(b->cprev != nil)
		b->cprev->cnext = b->cnext;
	b->cnext = nil;
	b->cprev = nil;		
}

void
lrutop(Blk *b)
{
	qlock(&fs->lrulk);
	/*
	 * Someone got in first and did a
	 * cache lookup; we no longer want
	 * to put this into the LRU, because
	 * its now in use.
	 */
	assert(b->magic == Magic);
	if(b->ref != 0){
		qunlock(&fs->lrulk);
		return;
	}
	lrudel(b);
	if(fs->chead != nil)
		fs->chead->cprev = b;
	if(fs->ctail == nil)
		fs->ctail = b;
	b->cnext = fs->chead;
	fs->chead = b;
	rwakeup(&fs->lrurz);
	qunlock(&fs->lrulk);
}

void
lrubot(Blk *b)
{
	qlock(&fs->lrulk);
	/*
	 * Someone got in first and did a
	 * cache lookup; we no longer want
	 * to put this into the LRU, because
	 * its now in use.
	 */
	assert(b->magic == Magic);
	if(b->ref != 0){
		qunlock(&fs->lrulk);
		return;
	}
	lrudel(b);
	if(fs->ctail != nil)
		fs->ctail->cnext = b;
	if(fs->chead == nil)
		fs->chead = b;
	b->bp.addr = -1;
	b->bp.hash = -1;
	b->cprev = fs->ctail;
	fs->ctail = b;
	rwakeup(&fs->lrurz);
	qunlock(&fs->lrulk);
}

void
cacheins(Blk *b)
{
	Bucket *bkt;
	u32int h;

	assert(b->magic == Magic);
	h = ihash(b->bp.addr);
	bkt = &fs->bcache[h % fs->cmax];
	lock(bkt);
	if(checkflag(b, Bcached)){
		unlock(bkt);
		return;
	}
	assert(b->hnext == nil);
	for(Blk *bb = bkt->b; bb != nil; bb = bb->hnext)
		assert(b != bb);
	setflag(b, Bcached);
	b->cached = getcallerpc(&b);
	b->hnext = bkt->b;
	bkt->b = b;
	unlock(bkt);
}

void
cachedel(vlong addr)
{
	Bucket *bkt;
	Blk *b, **p;
	u32int h;

	if(addr == -1)
		return;

	h = ihash(addr);
	bkt = &fs->bcache[h % fs->cmax];
	lock(bkt);
	p = &bkt->b;
	for(b = bkt->b; b != nil; b = b->hnext){
		if(b->bp.addr == addr){
			*p = b->hnext;
			clrflag(b, Bcached);
			b->uncached = getcallerpc(&addr);
			b->hnext = nil;
			break;
		}
		p = &b->hnext;
	}
	unlock(bkt);
}

Blk*
cacheget(vlong off)
{
	Bucket *bkt;
	u32int h;
	Blk *b;

	h = ihash(off);

	bkt = &fs->bcache[h % fs->cmax];

	lock(bkt);
	for(b = bkt->b; b != nil; b = b->hnext){
		if(b->bp.addr == off){
 			holdblk(b);
			qlock(&fs->lrulk);
			lrudel(b);
			qunlock(&fs->lrulk);
			b->lasthold = getcallerpc(&off);
			break;
		}
	}
	unlock(bkt);

	return b;
}

/*
 * Pulls the block from the bottom of the LRU for reuse.
 */
Blk*
cachepluck(void)
{
	Blk *b;

	qlock(&fs->lrulk);
	while(fs->ctail == nil)
		rsleep(&fs->lrurz);

	b = fs->ctail;
	assert(b->magic == Magic);
	assert(b->ref == 0);
	cachedel(b->bp.addr);
	lrudel(b);
	assert(!checkflag(b, Bcached));
	b->flag = 0;
	b->lasthold = 0;
	b->lastdrop = 0;
	b->freed = 0;
	b->hnext = nil;
	qunlock(&fs->lrulk);

	return  holdblk(b);
}