shithub: gefs

ref: 1a37a6bb30e050e20e53e36abeb8fda10398f3f9
dir: /load.c/

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

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

static int
rangecmp(Avl *a, Avl *b)
{
	if(((Arange*)a)->off < ((Arange*)b)->off)
		return -1;
	if(((Arange*)a)->off > ((Arange*)b)->off)
		return 1;
	return 0;
}

void
loadarena(Arena *a, Bptr hdbp, vlong asz)
{
	Blk *hd, *tl, *b;
	Bptr bp;

	/* try to load block pointers with consistency check */
	bp = hdbp;
	hd = nil;
	tl = nil;
	if(!waserror()){
		hd = getblk(bp, 0);
		poperror();
	}
	bp.addr += asz;
	if(!waserror()){
		tl = getblk(bp, 0);
		poperror();
	}

	/* if neither head nor tail is consistent, we're hosed */
	b = (hd != nil) ? hd : tl;
	if(b == nil)
		error(Efs);

	/* otherwise, we could have crashed mid-pass, just load the blocks */
	bp = hdbp;
	if(hd == nil)
		hd = getblk(bp, GBnochk);
	bp.addr += asz;
	if(tl == nil)
		tl = getblk(bp, GBnochk);

	unpackarena(a, b->data, Arenasz);
	if((a->free = avlcreate(rangecmp)) == nil)
		error(Enomem);
	a->hd = hd;
	a->tl = tl;
	a->used = a->size;
}

void
loadfs(char *dev)
{
	Mount *dump;
	Arena *a;
	Bptr bp;
	Tree *t;
	int i, k;

	if((dump = mallocz(sizeof(*dump), 1)) == nil)
		sysfatal("malloc: %r");
	if(waserror())
		sysfatal("load fs: %s", errmsg());
	snprint(dump->name, sizeof(dump->name), "dump");
	dump->ref = 1;
	dump->gen = -1;
	dump->root = &fs->snap;

	fs->snapmnt = dump;
	fs->gotinfo = 0;
	fs->narena = 1;
	if((fs->fd = open(dev, ORDWR)) == -1)
		sysfatal("open %s: %r", dev);
	bp = (Bptr){0, -1, -1};
	fs->sb0 = getblk(bp, GBnochk);
	bp = (Bptr){512*MiB, -1, -1};
	fs->sb1 = getblk(bp, GBnochk);
	unpacksb(fs, fs->sb0->buf, Blksz);
	if((fs->arenas = calloc(fs->narena, sizeof(Arena))) == nil)
		sysfatal("malloc: %r");
	for(i = 0; i < fs->narena; i++){
		a = &fs->arenas[i];
		memset(a, 0, sizeof(Arena));
		loadarena(a, fs->arenabp[i], fs->arenasz);
		a->reserve = a->size / 1024;
		if(a->reserve < 32*MiB)
			a->reserve = 32*MiB;
		if(!fs->gotinfo){
			if((fs->arenas = realloc(fs->arenas, fs->narena*sizeof(Arena))) == nil)
				sysfatal("malloc: %r");
			for(k = 1; k < fs->narena; k++)
				memset(&fs->arenas[k], 0, sizeof(Arena));
			fs->gotinfo = 1;
		}
	}
	for(i = 0; i < fs->narena; i++){
		a = &fs->arenas[i];
		a->logbuf[0] = cachepluck();
		a->logbuf[1] = cachepluck();
		a->logbuf[0]->bp = (Bptr){-1, -1, -1};
		a->logbuf[1]->bp = (Bptr){-1, -1, -1};
		loadlog(a, a->loghd);
	}

	if((t = opensnap("adm", nil)) == nil)
		sysfatal("load users: no adm label");
	loadusers(2, t);
	poperror();

	fprint(2, "load %s:\n", dev);
	fprint(2, "\tsnaptree:\t%B\n", fs->snap.bp);
	fprint(2, "\tnarenas:\t%d\n", fs->narena);
	fprint(2, "\tarenasz:\t%lld MiB\n", fs->arenasz/MiB);
	fprint(2, "\tnextqid:\t%lld\n", fs->nextqid);
	fprint(2, "\tnextgen:\t%lld\n", fs->nextgen);
	fprint(2, "\tblocksize:\t%lld\n", Blksz);
	fprint(2, "\tcachesz:\t%lld MiB\n", fs->cmax*Blksz/MiB);
	closesnap(t);
}