code: mafs

ref: 5c21eca829380c28769f79beabb7cc51f1787f97
dir: /ctl.c/

View raw version
#include "all.h"

enum { MAXARGS = 16 };

static int echo;

enum
{
	Cnoauth,
	Cchatty,
	Csync,
	Chalt,
	Cnewuser,
	Cusers,
	Cecho,
	Cdf,
	Cmax
};

int
cmdsync(Cmdbuf *)
{
	sync();
	dprint("sync\n");
	return 0;
}

int
cmdhalt(Cmdbuf *)
{
	shutdown();
	return 0;
}

int
cmdchatty(Cmdbuf *)
{
	extern int chatty9p;

	chatty9p = !chatty9p;
	return 0;
}

int
cmdnoauth(Cmdbuf *)
{
	noauth = 1;
	return 1;
}

int
cmdecho(Cmdbuf *cb)
{
	echo = strcmp(cb->f[1], "on") == 0;
	return 1;
}

int
cmdusers(Cmdbuf *)
{
	usersinit();
	return 0;
}

// TODO	{Cnewuser, "newuser", 0},
Cmdtab cmds[] = {
	{Cnoauth, "noauth", 1},
	{Cchatty, "chatty", 1},
	{Csync, "sync", 1},
	{Chalt, "halt", 1},
	{Cusers, "users", 1},
	{Cecho, "echo", 2},
};

// TODO	case Cnewuser:	cmdnewuser(cb); break;
int
runcmd(int idx, Cmdbuf *cb)
{
	switch(idx){
	case Cnoauth:	return cmdnoauth(cb); break;
	case Cchatty:	return cmdchatty(cb); break;
	case Csync:	return cmdsync(cb); break;
	case Chalt:	return cmdhalt(cb); break;
	case Cusers:	return cmdusers(cb); break;
	case Cecho:	return cmdecho(cb); break;
	default:
		dprint("unknown idx %d\n", idx);
	}
	return 0;
};

void
ctlread(Req *req)
{
	u64 free, used;
	char buf[1024];
	int n;

	free = nfrees(&frees);
	used = config.nblocks - free;
	if(config.size > TB)
	n = snprint(buf, 1024, "(blocks) free %ulld, used %ulld, total %ulld\n"
						"(GB) free %ulld, used %ulld, total %ulld\n"
						"(TB) free %ulld, used %ulld, total %ulld\n",
						free, used, config.nblocks,
						free * Rawblocksize / GB,
						used * Rawblocksize / GB,
						config.size / GB,
						free * Rawblocksize / TB,
						used * Rawblocksize / TB,
						config.size / TB
						);
	else if(config.size > GB)
	n = snprint(buf, 1024, "(blocks) free %ulld, used %ulld, total %ulld\n"
						"(MB) free %ulld, used %ulld, total %ulld\n"
						"(GB) free %ulld, used %ulld, total %ulld\n",
						free, used, config.nblocks,
						free * Rawblocksize / MB,
						used * Rawblocksize / MB,
						config.size / MB,
						free * Rawblocksize / GB,
						used * Rawblocksize / GB,
						config.size / GB
						);
	else if(config.size > MB)
	n = snprint(buf, 1024, "(blocks) free %ulld, used %ulld, total %ulld\n"
						"(MB) free %ulld, used %ulld, total %ulld\n",
						free, used, config.nblocks,
						free * Rawblocksize / MB,
						used * Rawblocksize / MB,
						config.size / MB
						);
	else
	n = snprint(buf, 1024, "(blocks) free %ulld, used %ulld, total %ulld\n"
						"(KB) free %ulld, used %ulld, total %ulld\n",
						free, used, config.nblocks,
						free * Rawblocksize / KB,
						used * Rawblocksize / KB,
						config.size / KB
						);
	if(req->ifcall.offset < n){
		req->ofcall.count = min(req->ifcall.count,n);
		memcpy(req->ofcall.data, buf+req->ifcall.offset, req->ofcall.count);
		req->ofcall.offset = req->ifcall.offset+req->ofcall.count;
	}
	respond(req, nil);
}

void
ctlwrite(Req *req)
{
	Cmdbuf *cb;
	Cmdtab *ct;

	/*if(chatty9p)
		dprint("consproc: >%s\n", req->ifcall.data);*/
	while((cb = parsecmd(req->ifcall.data, req->ifcall.count)) != nil){
		ct = lookupcmd(cb, cmds, nelem(cmds));
		if(ct != nil){
			req->ofcall.count = req->ifcall.count;
			req->ofcall.offset = req->ifcall.offset+req->ifcall.count;
			respond(req, nil);
			runcmd(ct->index, cb);
		}else{
			dprint("ct = nil\n");
			respond(req, errstring[Einval]);
		}
		free(cb);
	}
}

/* get rid of this after understanding what it does
static int
walkpath(Chan *ch, char *path, char **cr)
{
	char buf[Namelen], *p, *fp;

	fp = path;
	if(*path != '/'){
	noent:
		werrstr("%s: %s", fp, Enoent);
		return -1;
	}
	path++;
	for(;;){
		p = strchr(path, '/');
		if(p == nil){
			if(cr != nil){
				if(*path == 0){
					werrstr("%s: trailing slash", fp);
					return -1;
				}
				*cr = path;
				break;
			}
			p = path + strlen(path);
		}
		if(*path == '/'){
			path++;
			continue;
		}
		if(*path == 0)
			break;
		if(p - path >= Namelen)
			goto noent;
		memset(buf, 0, sizeof buf);
		memcpy(buf, path, p - path);
		if(chanwalk(ch, buf) <= 0){
			werrstr("%s: %r", fp);
			return -1;
		}
		if(*p == 0)
			break;
		path = p + 1;
	}
	return 1;
}

int
cmddebugdeind(int, char **argv)
{
	Chan *ch;
	Buf *b;
	Dentry *d;

	ch = chanattach(fsmain, 0);
	if(ch == nil)
		return -1;
	ch->uid = -1;
	if(walkpath(ch, argv[1], nil) < 0)
		goto error;
	rlock(fsmain);
	dprint("loc %ulld / %uld, offset %ulld\n", ch->loc->blk, ch->loc->deind, BLOCK * ch->loc->blk + (RBLOCK - BLOCK) + DENTRYSIZ * ch->loc->deind);
	b = getbuf(fsmain->d, ch->loc->blk, TDENTRY, 0);
	if(b == nil){
		runlock(fsmain);
		goto error;
	}
	d = &b->de[ch->loc->deind];
	dprint("name %s\n", d->name);
	dprint("uid %d, muid %d, gid %d\n", d->uid, d->muid, d->gid);
	dprint("mode %#o, qid %ulld, type %#x, version %d\n", d->mode, d->path, d->type, d->vers);
	dprint("size %d\n", d->size);
	dprint("atime %ulld, mtime %ulld\n", d->atime, d->mtime);
	putbuf(b);
	runlock(fsmain);
	chanclunk(ch);
	return 0;
error:
	chanclunk(ch);
	return -1;
}

int
cmddebugchdeind(int, char **argv)
{
	Chan *ch;
	uchar *c;
	Buf *b;
	int loc, new;

	loc = strtol(argv[2], nil, 0);
	new = strtol(argv[3], nil, 0);
	if(loc >= DENTRYSIZ)
		return -9001;
	ch = chanattach(fsmain, 0);
	if(ch == nil)
		return -1;
	ch->uid = -1;
	if(walkpath(ch, argv[1], nil) < 0)
		goto error;
	rlock(fsmain);
	b = getbuf(fsmain->d, ch->loc->blk, TDENTRY, 0);
	if(b == nil){
		runlock(fsmain);
		goto error;
	}
	c = (uchar *) &b->de[ch->loc->deind];
	dprint("loc %d, old value %#.2x, new value %#.2x\n", loc, c[loc], new);
	c[loc] = new;
	b->op |= BDELWRI;
	putbuf(b);
	runlock(fsmain);
	chanclunk(ch);
	return 0;
error:
	chanclunk(ch);
	return -1;
}

int
cmddebuggetblk(int argc, char **argv)
{
	Chan *ch;
	Buf *b;
	int rc;
	uvlong r, start, end, i;

	if(argc != 3 && argc != 4)
		return -9001;
	start = atoll(argv[2]);
	if(argc == 4)
		end = atoll(argv[3]);
	else
		end = start;
	ch = chanattach(fsmain, 0);
	if(ch == nil)
		return -1;
	ch->uid = -1;
	if(walkpath(ch, argv[1], nil) < 0)
		goto error;
	rlock(fsmain);
	b = getbuf(fsmain->d, ch->loc->blk, TDENTRY, 0);
	if(b == nil){
		runlock(fsmain);
		goto error;
	}
	for(i = start; i <= end; i++){
		rc = getblk(fsmain, ch->loc, b, i, &r, GBREAD);
		if(rc > 0)
			dprint("getblk %ulld = %ulld\n", i, r);
		if(rc == 0)
			dprint("getblk %ulld not found\n", i);
		if(rc < 0)
			dprint("getblk %ulld: %r\n", i);
	}
	putbuf(b);
	runlock(fsmain);
	chanclunk(ch);
	return 0;
error:
	chanclunk(ch);
	return -1;
} */