code: mafs

Download patch

ref: ec333deceba9dd45dabf4d36138398bdd020f449
parent: 1149d44033da561c89489db28f5f95d81f7251e1
author: 9ferno <gophone2015@gmail.com>
date: Thu Nov 17 21:37:38 EST 2022

stop the multi process srv process on shutdown

--- a/9p.c
+++ b/9p.c
@@ -2,7 +2,7 @@
 
 Tlock	*tlockhead = nil, *tlocktail = nil;
 QLock	tlock;
-extern int mpstarted;
+extern u32 mpsrvpid;
 extern u8 synchronouswrites;
 
 s32 readfile(u64 dblkno, u64 qpath, char *rbuf, s32 rbufsize, u64 offset, Req *req);
@@ -940,6 +940,14 @@
 			Nworkers, inv, n, w, buf.nworks, buf.hd, buf.tl);
 }
 
+/*
+	there are 2 ways to shutdown:
+	1. by unmounting and removing the /srv/mfs_service file (can be rm and unmount too)
+	2. by writing halt to /adm/ctl file. unmount() the file system to keep it clean.
+
+	In the second instance, fsend() is called and it stops the srv() process afterwards.
+	In the first instance, a worker needs to tell the srv() process to shutdown.
+ */
 void
 shutdown(void)
 {
@@ -952,7 +960,7 @@
 	fsok(1);
 //	showextents(&frees);
 //	dprint("&buf.isempty %#p\n", &buf.isempty);
-	if(mpstarted){
+	if(mpsrvpid){
 		qlock(&buf.lck);
 		rwakeupall(&buf.isempty);
 		qunlock(&buf.lck);
@@ -1009,7 +1017,7 @@
 void
 startproc(Buffer *b, int id)
 {
-	char name[16];
+	char name[128];
 
 	switch(rfork(RFPROC|RFMEM|RFFDG)){
 	case -1:
@@ -1016,12 +1024,12 @@
 		panic("can't fork");
 	case 0:
 		if(chatty9p > 1)
-		dprint("child %d pid: %d\n", id, getpid());
+			dprint("child %d pid: %d\n", id, getpid());
 		break;
 	default:
 		return;
 	}
-	snprint(name, 16, "%s worker %d", service, id);
+	snprint(name, 128, "%s worker %d", service, id);
 	procsetname(name);
 	work(b, id);
 	if(chatty9p)
@@ -1034,17 +1042,17 @@
 {
 	int i;
 
-	mpstarted=1;
+	mpsrvpid=getpid();
 	initworks(&buf);
 	if(chatty9p > 1)
-	dprint("parent pid: %d\n", getpid());
-	for(i = 0; i < Nworkers; i++){
+		dprint("srv() parent process pid: %d\n", mpsrvpid);
+	for(i = 0; i < Nworkers; i++)
 		startproc(&buf, i);
-	}
 }
 
+/* read the comment above shutdown() to understand how this works */
 void
-fshalt(Srv *)
+fsend(Srv *)
 {
 	if(shuttingdown == 0)
 		shutdown();
@@ -1122,7 +1130,7 @@
 	.remove = bfsremove,
 
 	.start = fsstart,
-	.end = fshalt,
+	.end = fsend,
 };
 
 /* usrv is a uni process server */
@@ -1143,7 +1151,7 @@
 	.wstat = fswstat,
 	.remove = fsremove,
 
-	.end = fshalt,
+	.end = fsend,
 };
 
 /*
--- a/blk.c
+++ b/blk.c
@@ -82,7 +82,7 @@
 	Content *c;
 
 	c = (Content*)buf;
-	fprint(fd, "%s", c->buf);
+	fprint(fd, "%s", (s8*)c->buf);
 }
 
 void
--- a/ctl.c
+++ b/ctl.c
@@ -6,14 +6,12 @@
 
 enum
 {
-	Cnoauth,
+	Cdf,
 	Cchatty,
 	Csync,
 	Chalt,
-	Cnewuser,
 	Cusers,
 	Cecho,
-	Cdf,
 	Cmax
 };
 
@@ -62,9 +60,7 @@
 	return 0;
 }
 
-// TODO	{Cnewuser, "newuser", 0},
 Cmdtab cmds[] = {
-	{Cnoauth, "noauth", 1},
 	{Cchatty, "chatty", 1},
 	{Csync, "sync", 1},
 	{Chalt, "halt", 0},
@@ -72,19 +68,17 @@
 	{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);
+		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 idx;
 };
@@ -152,7 +146,7 @@
 	respond(req, nil);
 }
 
-int mpstarted = 0;
+u32 mpsrvpid = 0;
 void
 ctlwrite(Req *req)
 {
@@ -172,10 +166,16 @@
 			respond(req, nil);
 			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{
+				if(mpsrvpid &&
+					snprint(srvfilename, Namelen, "/srv/%s", service) > 5){
+					n = remove(srvfilename);
+					close(req->srv->infd);
+					close(req->srv->outfd);
+					if(mpsrvpid > 0){
+						postnote(PNPROC, mpsrvpid, "halt");
+						mpsrvpid = 0;
+					}
+				}else{
 					close(1);
 					close(0);
 				}
@@ -182,7 +182,6 @@
 				exits(nil);
 			}
 		}else{
-			dprint("ct = nil\n");
 			respond(req, errstring[Einval]);
 		}
 		free(cb);
--- a/docs/mafs.ms
+++ b/docs/mafs.ms
@@ -1204,7 +1204,7 @@
 .sp
 .nf
 	dd -if /dev/zero -of disk.file -bs 512 -count 4096;
-	mount -c <{disk/mafs -s -r mafs_disk.file \\
+	mount -c <{disk/mafs -s -r mafs_disk.file disk.file \\
 		  <[0=1]} /n/mafs_disk.file
 .fi
 .sp
@@ -1467,6 +1467,7 @@
 		192.80 MB/s
 	dd -if /dev/zero -of /n/ramfs/file -count 700 -bs 1m
 
+	mount -c <{disk/mafs -s -r mafs_disk.file <[0=1]} /n/mafs_disk.file
 	disk/mfs -r mfs_ramfs_file /n/ramfs/file
 	mount -c /srv/mfs_ramfs_file /n/mfs_ramfs_file
 	cat /dev/zero | tput -p > /n/mfs_ramfs_file/zeros.file
@@ -1490,7 +1491,8 @@
 		36.25 MB/s
 		36.07 MB/s
 	echo halt >> /n/mfs_ramfs_file/adm/ctl; lc /srv
-	unmount /n/mfs_ramfs
+	unmount /n/mfs_ramfs_file
+	rm /srv/mfs_ramfs_file
 
 	disk/mafs -r mafs_ramfs_file /n/ramfs/file
 	mount -c /srv/mafs_ramfs_file /n/mafs_ramfs_file
--- a/used.c
+++ b/used.c
@@ -92,7 +92,7 @@
 {
 	u8 buf[Rawblocksize], *cbuf;
 	Content *t, *ct;
-	u64 cblkno, *bufa;
+	u64 cblkno;
 	int i;
 
 	devread(blkno, buf, blklen(tag));