code: mafs

ref: 16f09290e03ec4fba68b6587dc01c13299c2a6f6
dir: /used.c/

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

/*
	go through all the blocks and write out all the used block numbers
	to be used by the checker
	Starting from root, walk down each dentry printing out the linked blocks with invalid tags
 */

Extents useds = {0};
int chatty9p = 0;
char *devfile = nil;
void walkdirectory(u64 blkno);
void walkfile(u64 blkno);
int checkdentry(u64 blkno, u8 tag, u64 qpath);

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

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

	ARGBEGIN{
	default:	usage();
	case 'D':	chatty9p= 8
	; 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/Unit);

	initextents(&useds);
	checkdentry(Bdmagic, Tdentry, Qpmagic);
	walkdirectory(Bdroot);
	close(devfd);
	showextents(1, "", &useds);
	exits(0);
}

int
checkvalid(u64 blkno, u8 dtag, u64 dpath, u8 tag, u64 qpath)
{
	if(dtag != tag || dpath != qpath){
		/* if(chatty9p) */
		fprint(2, "checkblock invalid %llud tag/path expected %s/%llud actual %s/%llud\n",
			blkno, tagnames[tag], qpath, tagnames[dtag], dpath);
		fprint(2, "used: %llud\n", blkno);
		return 0;
	}
	if(chatty9p)
		print("blkno %llud tag %s\n", blkno, tagnames[dtag]);
	// print("%llud\n", blkno);
	add(&useds, blkno, blklen(tag));
	return 1;
}

int
checkdentry(u64 blkno, u8 tag, u64 qpath)
{
	u8 buf[Rawblocksize];
	Dentry *d;

	devread(blkno, buf, Dentryunits);
	d = (Dentry*)buf;
	return checkvalid(blkno, d->tag, d->path, tag, qpath);
}

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

	devread(blkno, buf, blklen(tag));
	t = (Content*)buf;
	if(checkvalid(blkno, t->tag, t->path, tag, qpath)){
		if(tag == Tind0){
			for(i = 0; i<Nindperblock; i++){
				cblkno = t->bufa[i];
				if(cblkno == 0)
					return;
				cbuf = malloc(blklen(bottomtag)*Unit);
				devread(cblkno, cbuf, blklen(bottomtag));
				ct = (Content*)cbuf;
				if(ct->tag == Tdentry && ct->tag == bottomtag)
					walkdirectory(cblkno);
				else
					checkvalid(cblkno, ct->tag, ct->path, bottomtag, qpath);
				free(cbuf);
			}
		}else{
			for(i = 0; i<Nindperblock; i++){
				cblkno = t->bufa[i];
				if(cblkno == 0)
					return;
				walkindir(cblkno, tag-1,  bottomtag, qpath);
			}
		}
	}
	return;
}

void
walkdirectory(u64 blkno)
{
	u8 buf[Rawblocksize], *cbuf;
	Dentry *d, *cd;
	Content *ct;
	u64 cblkno;
	int i;

	devread(blkno, buf, Dentryunits);
	d = (Dentry*)buf;
	if(chatty9p)
		print("walkdirectory %llud tag %s name %s d->qid.path %llud\n",
				blkno, tagnames[d->tag], d->name, d->qid.path);
	if(d->tag != Tdentry || d->path != d->qid.path){
		if(chatty9p)
			print("walkdirectory invalid %llud tag/path expected %s/%llud actual %s/%llud\n",
					blkno, tagnames[Tdentry], d->qid.path, tagnames[d->tag], d->path);
		fprint(2, "%llud\n", blkno);
	}else{
		// print("%llud\n", blkno);
		add(&useds, blkno, Dentryunits);
	}
	/* do not list the data blocks used by /adm/frees
		as they are considered to be free blocks */
	if(blkno == Bdfrees)
		return;
	for(i = 0; i<Ndblock; i++){
		cblkno = d->dblocks[i];
		if(cblkno == 0)
			return;
		cbuf = malloc(Dentrysize);
		devread(cblkno, cbuf, Dentryunits);
		cd = (Dentry*)cbuf;
		if((cd->mode & DMDIR) > 0)
			walkdirectory(cblkno);
		else
			walkfile(cblkno);
		free(cbuf);
	}
	cbuf = malloc(Rawblocksize);
	for(i = 0; i<Niblock; i++){
		cblkno = d->iblocks[i];
		if(cblkno == 0)
			return;
		devread(cblkno, cbuf, Dentryunits);
		ct = (Content*)cbuf;
		if(ct->tag == Tind0+i){
			walkindir(cblkno, Tind0+i, Tdentry, d->qid.path);
		}else{
			fprint(2, "invalid indir tag %llud\n", cblkno);
			fprint(2, "%llud\n", cblkno);
		}
	}
	free(cbuf);
	return;
}

void
walkfile(u64 blkno)
{
	u8 buf[Rawblocksize], *cbuf;
	Dentry *d;
	Content *ct;
	u64 cblkno;
	int i;

	devread(blkno, buf, Dentryunits);
	d = (Dentry*)buf;
	if(chatty9p)
		print("walkfile %llud tag %s name %s d->qid.path %llud\n",
				blkno, tagnames[d->tag], d->name, d->qid.path);
	if(d->tag != Tdentry || d->path != d->qid.path){
		if(chatty9p)
			print("walkfile invalid %llud tag/path expected %s/%llud actual %s/%llud\n",
					blkno, tagnames[Tdentry], d->qid.path, tagnames[d->tag], d->path);
		fprint(2, "%llud\n", blkno);
	}else{
		// print("%llud\n", blkno);
		add(&useds, blkno, Dentryunits);
	}
	/* do not list the data blocks used by /adm/frees
		as they are considered to be free blocks */
	if(blkno == Bdfrees)
		return;
	if(d->size <= Ddatasize)
		return;
	for(i = 0; i<Ndblock; i++){
		cblkno = d->dblocks[i];
		if(cblkno == 0)
			return;
		cbuf = malloc(Rawblocksize);
		devread(cblkno, cbuf, Rawblockunits);
		ct = (Content*)cbuf;
		checkvalid(cblkno, ct->tag, ct->path, Tdata, d->qid.path);
		free(cbuf);
	}
	cbuf = malloc(Rawblocksize);
	for(i = 0; i<Niblock; i++){
		cblkno = d->iblocks[i];
		if(cblkno == 0)
			return;
		devread(cblkno, cbuf, Rawblockunits);
		ct = (Content*)cbuf;
		if(ct->tag == Tind0+i){
			walkindir(cblkno, Tind0+i, Tdata, d->qid.path);
		}else{
			fprint(2, "invalid indir tag %llud\n", cblkno);
			fprint(2, "%llud\n", cblkno);
		}
	}
	free(cbuf);
	return;
}