code: mafs

ref: b5d191e8cfa4fac05a02ea309df2541a2652ff23
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 walkdentry(u64 blkno);
int checkblock(u64 blkno, s16 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/Rawblocksize);

	initextents(&useds);
	checkblock(Bmagicb, Tmagic, Qpmagic);
	walkdentry(Broot);
	close(devfd);
	showextents(1, "", &useds);
	exits(0);
}

int
checkvalid(u64 blkno, Content *t, s16 tagtype, u64 qpath)
{
	if(t->type != tagtype || t->path != qpath){
		/* if(chatty9p) */
		fprint(2, "checkblock invalid %llud tag/path expected %s/%llud actual %s/%llud\n",
			blkno, tagnames[tagtype], qpath, tagnames[t->type], t->path);
		fprint(2, "used: %llud\n", blkno);
		return 0;
	}
	if(chatty9p)
		print("blkno %llud tag %s\n", blkno, tagnames[t->type]);
	// print("%llud\n", blkno);
	add(&useds, blkno, 1);
	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
walkindir(u64 blkno, u16 tag, u16 bottomtag, u64 qpath)
{
	u8 buf[Rawblocksize], *cbuf;
	Content *t, *ct;
	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;
				cbuf = malloc(Rawblocksize);
				devread(cblkno, cbuf);
				ct = (Content*)cbuf;
				if(ct->type == Tdentry && ct->type == bottomtag)
					walkdentry(cblkno);
				else
					checkvalid(cblkno, ct, bottomtag, qpath);
				free(cbuf);
			}
		}else{
			bufa = (u64*)buf;
			cbuf = malloc(Rawblocksize);
			for(i = 0; i<Nindperblock; i++){
				cblkno = bufa[i];
				if(cblkno == 0)
					return;
				devread(cblkno, cbuf);
				/* check tag */
				walkindir(cblkno, tag-1,  bottomtag, qpath);
			}
			free(cbuf);
		}
	}
	return;
}

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

	devread(blkno, buf);
	t = (Content*)buf;
	d = (Dentry*)buf;
	isdir = (d->mode & DMDIR) > 0;
	if(chatty9p)
		print("walkdentry %llud tag %s name %s d->qid.path %llud\n",
				blkno, tagnames[t->type], d->name, d->qid.path);
	if(t->type != Tdentry || t->path != d->qid.path){
		if(chatty9p)
			print("walkdentry invalid %llud tag/path expected %s/%llud actual %s/%llud\n",
					blkno, tagnames[Tdentry], d->qid.path, tagnames[t->type], t->path);
		fprint(2, "%llud\n", blkno);
	}else{
		// print("%llud\n", blkno);
		add(&useds, blkno, 1);
	}
	/* 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(Rawblocksize);
		devread(cblkno, cbuf);
		ct = (Content*)cbuf;
		if(isdir)
			walkdentry(cblkno);
		else
			checkvalid(cblkno, ct, 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);
		ct = (Content*)cbuf;
		if(ct->type == Tind0+i){
			walkindir(cblkno, Tind0+i, isdir ? Tdentry : Tdata, d->qid.path);
		}else{
			fprint(2, "invalid indir tag %llud\n", cblkno);
			fprint(2, "%llud\n", cblkno);
		}
	}
	free(cbuf);
	return;
}