code: mafs

ref: 0ca333a4b367336a5ecaabb14475f50dcac7b77d
dir: /free.c/

View raw version
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"
#include "extents.h"

/*
	go through the contents of Bfrees and write out the list of free blocks
	used by reconcile.c to identify discrepancies
 */

Extents frees;
int chatty9p = 0;
char *devfile = nil;
void walkdentry(u64 blkno);
int checkblock(u64 blkno, s16 tag, u64 qpath);
void getfrees(u64 dblkno);

static void
usage(void)
{
	fprint(2, "usage: free [-D ] fsfile\n");
	exits("usage");
}

void
main(int argc, char *argv[])
{
	u64 size;

	ARGBEGIN{
	default:	usage();
	case 'D':	chatty9p++; break;
	}ARGEND

	if(argc != 1)
		usage();

	devfile = argv[0];
	if(devfile == nil)
		sysfatal("no disk file");

	if (access(devfile, AREAD) == -1)
		sysfatal("%s cannot access device", devfile);

	size = devinit(devfile);
	if(size == 0)
		panic("null size %s", devfile);
	if(chatty9p)
		print("%s %llud bytes %llud blocks\n", devfile, size, size/Rawblocksize);

	checkblock(Bdfrees, Tdentry, Qpfrees);
	initextents(&frees);
	getfrees(Bdfrees);
// showextents("after getfrees", &frees);
	showblocknos(1, &frees);
	exits(0);
}

int
checkvalid(u64 blkno, Content *t, s16 tag, u64 qpath)
{
	if(t->type != tag || t->path != qpath){
		/* if(chatty9p) */
			fprint(2, "check invalid %llud tag/path expected %s/%llud actual %s/%llud\n",
					blkno, tagnames[tag], qpath, tagnames[t->type], t->path);
		fprint(2, "free: %llud\n", blkno);
		return 0;
	}
	if(chatty9p)
	print("%llud\n", blkno);
	return 1;
}

int
checkblock(u64 blkno, s16 tag, u64 qpath)
{
	u8 buf[Rawblocksize];
	Content *t;

	devread(blkno, buf);
	t = (Content*)buf;
	return checkvalid(blkno, t, tag, qpath);
}

void
loadfreeextents(u64 blkno)
{
	s8 *buf;

	buf = emalloc(Rawblocksize);
	devread(blkno, buf);
	loadextents(&frees, buf, (Rawblocksize)-sizeof(Tag));
	free(buf);
}

void
walkindir(u64 blkno, u16 tag, u16 bottomtag, u64 qpath)
{
	u8 buf[Rawblocksize], cbuf[Rawblocksize];
	Content *t;
	u64 cblkno, *bufa;
	int i;

	devread(blkno, buf);
	t = (Content*)buf;
	if(checkvalid(blkno, t, tag, qpath)){
		if(t->type == Tind0){
			bufa = (u64*)buf;
			for(i = 0; i<Nindperblock; i++){
				cblkno = bufa[i];
				if(cblkno == 0)
					return;
				loadfreeextents(cblkno);
			}
		}else{
			bufa = (u64*)buf;
			for(i = 0; i<Nindperblock; i++){
				cblkno = bufa[i];
				if(cblkno == 0)
					return;
				devread(cblkno, cbuf);
				/* check tag */
				walkindir(cblkno, tag-1,  bottomtag, qpath);
			}
		}
	}
}

void
getfrees(u64 dblkno)
{
	u64 size;
	u8 buf[Rawblocksize], cbuf[Rawblocksize];
	Dentry *d;
	Content *t;
	u64 cblkno;
	int i;

	devread(dblkno, buf);
	t = (Content*)buf;
	d = (Dentry*)buf;
	size = d->size;
	if(size == 0)
		panic("loadfreeextents size == 0");

	if(checkvalid(dblkno, t, Tdentry, d->qid.path)){
		for(i = 0; i<Ndblock; i++){
			cblkno = d->dblocks[i];
			if(cblkno == 0)
				return;
			loadfreeextents(cblkno);
		}
		for(i = Tind0; i<Niblock+Tind0; i++){
			cblkno = d->iblocks[i];
			if(cblkno == 0)
				return;
			devread(cblkno, cbuf);
			walkindir(cblkno, i, Tdata, Qpfrees);
		}
	}
}