code: mafs

ref: 9b76881a8e11c44aae5dfd447e1cd1bc074ec9f3
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/Blocksize);

	initextents(&useds, "useds", nil);
	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, u16 len)
{
	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, len);
	return 1;
}

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

	devread(blkno, buf, Metadataunits);
	recentmetadata(buf, &d, nil);
	return checkvalid(blkno, d->tag, d->path, tag, qpath, Metadataunits);
}

/* not checking the ending path as it would take too long */
void
walkindir(u64 blkno, u16 tag, u16 bottomtag, u64 qpath)
{
	u8 buf[Metadatablocksize], cbuf[Metadatablocksize];
	u64 ebuf[Nu64perblock];
	Indirect *t;
	Data *da;
	u64 cblkno, path;
	int i;
	Dentry *cd;

	devread(blkno, buf, Metadataunits);
	recentmetadata(buf, &t, nil);
	if(checkvalid(blkno, t->tagi, t->path, tag, qpath, Metadataunits)){
		if(tag == Tind0){
			for(i = 0; i<Nindperblock; i++){
				cblkno = t->bufa[i];
				if(cblkno == 0)
					return;
				if(bottomtag == Tdentry){
					devread(cblkno, cbuf, Metadataunits);
					recentmetadata(cbuf, &cd, nil);
					if((cd->mode & DMDIR) > 0)
						walkdirectory(cblkno);
					else
						walkfile(cblkno);
				}else{
					devread(cblkno, cbuf, 1);
					da = (Data*)cbuf;
					devread(cblkno+da->len-1, ebuf, 1);
					path = ebuf[Dpathidx];
					checkvalid(cblkno, da->tag, path, Tdata, qpath, da->len);
				}
			}
		}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[Metadatablocksize], cbuf[Metadatablocksize];
	Dentry *d, *cd;
	Indirect *it;
	u64 cblkno;
	int i;

	devread(blkno, buf, Metadataunits);
	recentmetadata(buf, &d, nil);
	if(chatty9p)
		print("walkdirectory %llud tag %s name %s d->path %llud\n",
				blkno, tagnames[d->tag], d->name, d->path);
	if(d->tag != Tdentry || d->path != d->qpath){
		if(chatty9p)
			print("walkdirectory invalid %llud tag/path expected %s/%llud actual %s/%llud\n",
					blkno, tagnames[Tdentry], d->qpath, tagnames[d->tag], d->path);
		fprint(2, "%llud\n", blkno);
	}else{
		// print("%llud\n", blkno);
		add(&useds, blkno, Metadataunits);
	}
	/* 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;
		devread(cblkno, cbuf, Metadataunits);
		recentmetadata(cbuf, &cd, nil);
		if((cd->mode & DMDIR) > 0)
			walkdirectory(cblkno);
		else
			walkfile(cblkno);
	}
	for(i = 0; i<Niblock; i++){
		cblkno = d->iblocks[i];
		if(cblkno == 0)
			return;
		devread(cblkno, cbuf, Metadataunits);
		recentmetadata(cbuf, &it, nil);
		if(it->tagi == Tind0+i){
			walkindir(cblkno, Tind0+i, Tdentry, d->qpath);
		}else{
			fprint(2, "invalid indir tag %llud\n", cblkno);
			fprint(2, "%llud\n", cblkno);
		}
	}
	return;
}

/* not checking the ending path as it would take too long */
void
walkfile(u64 blkno)
{
	u8 buf[Metadatablocksize], cbuf[Metadatablocksize];
	u64 ebuf[Nu64perblock];
	Dentry *d;
	Data *ct;
	Indirect *it;
	u64 cblkno, path;
	int i;

	devread(blkno, buf, Metadataunits);
	recentmetadata(buf, &d, nil);
	if(chatty9p)
		print("walkfile %llud tag %s name %s d->qid.path %llud\n",
				blkno, tagnames[d->tag], d->name, d->qpath);
	if(d->tag != Tdentry || d->path != d->qpath){
		if(chatty9p)
			print("walkfile invalid %llud tag/path expected %s/%llud actual %s/%llud\n",
					blkno, tagnames[Tdentry], d->qpath, tagnames[d->tag], d->path);
		fprint(2, "%llud\n", blkno);
	}else{
		// print("%llud\n", blkno);
		add(&useds, blkno, Metadataunits);
	}
	/* 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;
		devread(cblkno, cbuf, 1);
		ct = (Data*)cbuf;
		devread(cblkno+ct->len-1, ebuf, 1);
		path = ebuf[Dpathidx];
		checkvalid(cblkno, ct->tag, path, Tdata, d->qpath, ct->len);
	}
	for(i = 0; i<Niblock; i++){
		cblkno = d->iblocks[i];
		if(cblkno == 0)
			return;
		devread(cblkno, cbuf, Metadataunits);
		recentmetadata(cbuf, &it, nil);
		if(it->tagi == Tind0+i){
			walkindir(cblkno, Tind0+i, Tdata, d->qpath);
		}else{
			fprint(2, "invalid indir tag of block %llud %d %s\n",
					cblkno, it->tagi, tagnames[it->tagi]);
			fprint(2, "%llud\n", cblkno);
		}
	}
	return;
}