code: mafs

Download patch

ref: 0e95da7146265fe0f54745c89f93d7685a68e1bd
parent: ec41f2072998cb42f5ce26d8f462fb21707ef4c1
author: 9ferno <gophone2015@gmail.com>
date: Thu Oct 20 06:54:18 EDT 2022

wait till the writes are complete before responding to halt

--- a/9p.c
+++ b/9p.c
@@ -1,6 +1,6 @@
 #include "all.h"
 
-int mpstarted = 0;
+extern int mpstarted;
 
 s32 readfile(u64 dblkno, u64 qpath, char *rbuf, s32 rbufsize, u64 offset);
 s32 writefile(u64 dblkno, u64 qpath, s16 uid, char *wbuf, s32 wbufsize, u64 offset);
@@ -268,7 +268,6 @@
 
 	if(shuttingdown){
 		respond(req, errstring[Eshutdown]);
-		shutdown();
 		return;
 	}
 	if(readonly){
@@ -309,7 +308,6 @@
 
 	if(shuttingdown){
 		respond(req, errstring[Eshutdown]);
-		shutdown();
 		return;
 	}
 	if(readonly){
@@ -342,7 +340,6 @@
 	Iobuf *dbuf, *pbuf, *cbuf;
 
 	if(shuttingdown){
-		shutdown();
 		return nil;
 	}
 	if((fid->qid.type&QTDIR) == 0)
@@ -402,7 +399,6 @@
 	Aux *o;
 
 	if(shuttingdown){
-		shutdown();
 		return nil;
 	}
 	o = oldfid->aux;
@@ -452,7 +448,6 @@
 	/* no create's when shutting down */
 	if(shuttingdown){
 		respond(req, errstring[Eshutdown]);
-		shutdown();
 		return;
 	}
 	fid = req->fid;
@@ -811,6 +806,9 @@
 
 	if(shuttingdown){
 		respond(r, errstring[Eshutdown]);
+		qlock(&b->lck);
+		rwakeupall(&b->isempty);
+		qunlock(&b->lck);
 	}
 	w.f = f;
 	w.r = r;
--- a/blk.c
+++ b/blk.c
@@ -4,16 +4,16 @@
 #include "fns.h"
 
 void
-showind0(u8 *buf)
+showind0(int fd, u8 *buf)
 {
 	int j;
 	Spanid *s;
 
 	for(j = 0, s=(Spanid*)buf; j<Nspanidperblock; j++)
-		print("	%d %llud %d\n", j, s[j].blkno, s[j].len);
+		fprint(fd, "	%d %llud %d\n", j, s[j].blkno, s[j].len);
 }
 void
-showind(u8 *buf)
+showind(int fd, u8 *buf)
 {
 	int j;
 	u64 *lbuf;
@@ -20,84 +20,84 @@
 
 	lbuf = (u64*)buf;
 	for(j = 0; j<Nindperblock; j++)
-		print("	%d %llud\n", j, lbuf[j]);
+		fprint(fd, "	%d %llud\n", j, lbuf[j]);
 }
 void
-showdentry(u8 *buf)
+showdentry(int fd, u8 *buf)
 {
 	int j;
 	Dentry *d;
 
 	d = (Dentry*)buf;
-	print("qid.version %ud\n", d->qid.version);
-	print("qid.path %llud\n", d->qid.path);
-	print("size %llud\n", d->size);
-	print("pdblkno %llud\n", d->pdblkno);
-	print("pqpath %llud\n", d->pqpath);
-	print("mtime %llud\n", d->mtime);
-	print("mode %uo\n", d->mode);
-	print("uid %d\n", d->uid);
-	print("gid %d\n", d->gid);
-	print("muid %d\n", d->muid);
-	print("direct spans\n");
+	fprint(fd, "qid.version %ud\n", d->qid.version);
+	fprint(fd, "qid.path %llud\n", d->qid.path);
+	fprint(fd, "size %llud\n", d->size);
+	fprint(fd, "pdblkno %llud\n", d->pdblkno);
+	fprint(fd, "pqpath %llud\n", d->pqpath);
+	fprint(fd, "mtime %llud\n", d->mtime);
+	fprint(fd, "mode %uo\n", d->mode);
+	fprint(fd, "uid %d\n", d->uid);
+	fprint(fd, "gid %d\n", d->gid);
+	fprint(fd, "muid %d\n", d->muid);
+	fprint(fd, "direct spans\n");
 	for(j = 0; j<Ndspanid; j++)
-		print("	%d %llud %d\n", j, d->dspans[j].blkno, d->dspans[j].len);
-	print("indirect blocks\n");
+		fprint(fd, "	%d %llud %d\n", j, d->dspans[j].blkno, d->dspans[j].len);
+	fprint(fd, "indirect blocks\n");
 	for(j = 0; j<Niblock; j++)
-		print("	%d %llud\n", j, d->iblocks[j]);
-	print("name %s\n", d->name);
+		fprint(fd, "	%d %llud\n", j, d->iblocks[j]);
+	fprint(fd, "name %s\n", d->name);
 }
 void
-showmagic(u8 *buf)
+showmagic(int fd, u8 *buf)
 {
-	print("%s", (char*)buf+256);
+	fprint(fd, "%s", (char*)buf+256);
 }
 void
-showconfig(u8 *buf)
+showconfig(int fd, u8 *buf)
 {
-	print("%s", (char*)buf);
+	fprint(fd, "%s", (char*)buf);
 }
 void
-showsuper(u8 *buf)
+showsuper(int fd, u8 *buf)
 {
 	Superb *s;
 
 	s = (Superb*)buf;
-	print("start %llud\n", s->start);
-	print("tfree %llud\n", s->tfree);
-	print("qidgen %llud\n", s->qidgen);
-	print("frees %llud\n", s->frees);
-	print("fsok %llud\n", s->fsok);
+	fprint(fd, "start %llud\n", s->start);
+	fprint(fd, "tfree %llud\n", s->tfree);
+	fprint(fd, "qidgen %llud\n", s->qidgen);
+	fprint(fd, "frees %llud\n", s->frees);
+	fprint(fd, "fsok %llud\n", s->fsok);
 }
 void
-showdata(u8 *buf)
+showdata(int fd, u8 *buf)
 {
-	print("%s", (char*)buf);
+	fprint(fd, "%s", (char*)buf);
 }
 
 void
-showblock(u8 *buf)
+showblock(int fd, u8 *buf)
 {
 	Tag *t;
 
 	t = (Tag*)buf;
 	if(t->type < Maxtind)
-		print("%s %d %llud\n", tagnames[t->type], t->len, t->path);
+		fprint(fd, "%s %d %llud\n", tagnames[t->type], t->len, t->path);
 	if(t->type == Tdentry){
-		showdentry(buf+sizeof(Tag));
+		showdentry(fd, buf+sizeof(Tag));
 	}else if(t->path == Qpmagic){
-		showmagic(buf+sizeof(Tag));
+		showmagic(fd, buf+sizeof(Tag));
 	}else if(t->path == Qpconfig || t->path == Qpconfig0 || t->path == Qpconfig1){
-		showconfig(buf+sizeof(Tag));
+		showconfig(fd, buf+sizeof(Tag));
 	}else if(t->path == Qpsuper || t->path == Qpsuper0 || t->path == Qpsuper1){
-		showsuper(buf+sizeof(Tag));
+		showsuper(fd, buf+sizeof(Tag));
 	}else if(t->type == Tdata){
-		showdata(buf+sizeof(Tag));
+		showdata(fd, buf+sizeof(Tag));
 	}else if(t->type == Tind0){
-		showind0(buf+sizeof(Tag));
+		showind0(fd, buf+sizeof(Tag));
 	}else if(t->type > Tind0 && t->type < Maxtind){
-		showind(buf+sizeof(Tag));
+		showind(fd, buf+sizeof(Tag));
 	}else if(t->type != 0 || t->path != 0 || t->dirty != 0 || t->len != 0){
-		print("unknown tag type %d path %llud\n", t->type, t->path);
+		fprint(fd, "unknown tag type %d path %llud\n", t->type, t->path);
 	}
 }
--- a/block.c
+++ b/block.c
@@ -47,6 +47,6 @@
 	}
 
 	devread(blkno, buf, 1);
-	showblock(buf);
+	showblock(2, buf);
 	exits(0);
 }
--- a/ctl.c
+++ b/ctl.c
@@ -29,7 +29,7 @@
 cmdhalt(Cmdbuf *)
 {
 	shutdown();
-	return 0;
+	return Chalt;
 }
 
 int
@@ -67,7 +67,7 @@
 	{Cnoauth, "noauth", 1},
 	{Cchatty, "chatty", 1},
 	{Csync, "sync", 1},
-	{Chalt, "halt", 1},
+	{Chalt, "halt", 0},
 	{Cusers, "users", 1},
 	{Cecho, "echo", 2},
 };
@@ -86,7 +86,7 @@
 	default:
 		dprint("unknown idx %d\n", idx);
 	}
-	return 0;
+	return idx;
 };
 
 void
@@ -150,21 +150,35 @@
 	respond(req, nil);
 }
 
+int mpstarted = 0;
 void
 ctlwrite(Req *req)
 {
 	Cmdbuf *cb;
 	Cmdtab *ct;
+	int n;
+	char srvfilename[Namelen];
 
-	/*if(chatty9p)
-		dprint("consproc: >%s\n", req->ifcall.data);*/
+	/*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;
+			n = runcmd(ct->index, cb);
+			dprint("before respond\n");
 			respond(req, nil);
-			runcmd(ct->index, cb);
+			dprint("after respond\n");
+			if(ct->index == Chalt || n == Chalt){
+				/* /srv/mafs_service file will not exist when mounted with -s */
+				if(mpstarted &&
+					sprint(srvfilename, "/srv/%s", service) > 5)
+					remove(srvfilename);
+				else
+					close(1);
+				exits(nil);
+			}
 		}else{
 			dprint("ct = nil\n");
 			respond(req, errstring[Einval]);
--- a/fns.h
+++ b/fns.h
@@ -14,14 +14,14 @@
 int devwritedirtyclear(u64 blkno);
 
 /* show blocks */
-void	showblock(u8 *buf);
-void	showdentry(u8 *buf);
-void	showmagic(u8 *buf);
-void	showconfig(u8 *buf);
-void	showsuper(u8 *buf);
-void	showdata(u8 *buf);
-void	showind0(u8 *buf);
-void	showind(u8 *buf);
+void	showblock(int fd, u8 *buf);
+void	showdentry(int fd, u8 *buf);
+void	showmagic(int fd, u8 *buf);
+void	showconfig(int fd, u8 *buf);
+void	showsuper(int fd, u8 *buf);
+void	showdata(int fd, u8 *buf);
+void	showind0(int fd, u8 *buf);
+void	showind(int fd, u8 *buf);
 
 void	panic(char*, ...);
 int	fprint(int, char*, ...);
--- a/iobuf.c
+++ b/iobuf.c
@@ -140,9 +140,14 @@
 		Ncollisions is a soft limit.
 	 */
 	if(ncollisions >= Ncollisions){
+Another:
 		do{
 			p = s->back;
-			if(p->ref == 0 && canwlock(p)){
+			if(p->ref == 0 && p->dirties == 0 && canwlock(p)){
+				if(p->dirties > 0){
+					wunlock(p);
+					goto Another;
+				}
 				if(p->len != len){
 					free(p->xiobuf);
 					p->xiobuf = emalloc9p(len*Rawblocksize);
@@ -449,5 +454,5 @@
 			p->xiobuf,
 			getcallerpc(&p));
 	if(p->io != nil)
-		showblock((u8*)p->io);
+		showblock(2, (u8*)p->io);
 }
--- a/sub.c
+++ b/sub.c
@@ -127,8 +127,8 @@
 		/* clear the buf to avoid leaks on reuse */
 		memset(buf->io, 0, buf->len*Rawblocksize);
 		bfree(&frees, buf->blkno, buf->len);
-		putbuffree(buf);
 	}
+	putbuffree(buf);
 }
 
 /* add the block to the extents used to manage free blocks */
@@ -181,7 +181,7 @@
 	s->fsok = ok;
 	if(chatty9p > 1){
 		dprint("fsok ok %d\n", ok);
-		showsuper((u8*)s);
+		showsuper(2, (u8*)s);
 	}
 	putbuf(sb);
 }