code: mafs

Download patch

ref: 7754c42469ca6f6dff350f2d87e0903a5ce61c00
parent: 236a5fcadfeb27c1ffa2037db91486019f226d4f
author: 9ferno <gophone2015@gmail.com>
date: Sat Oct 15 02:39:47 EDT 2022

replaced Iobuf.flags with Iobuf.readonly

makes the code more comprehensible
Also, not reading from the disk for allocations of free blocks

--- a/9p.c
+++ b/9p.c
@@ -158,7 +158,7 @@
 
 	/* TODO cwfs/9p2.c, hjfs/fs2.c and kfs64.b do more here. Get that stuff in. */
 
-	dbuf = getbufchk(((Aux*)req->fid->aux)->dblkno, 1, Bmod,
+	dbuf = getbufchk(((Aux*)req->fid->aux)->dblkno, 1, Bwritable,
 						Tdentry, req->fid->qid.path);
 	if(dbuf == nil){
 		respond(req, errstring[Eperm]);
@@ -503,7 +503,7 @@
 	cbuf = nil;
 	USED(cbuf);
 	zblkno = 0;
-	dbuf = getbufchk(aux->dblkno, 1, Bmod, Tdentry, fid->qid.path);
+	dbuf = getbufchk(aux->dblkno, 1, Bwritable, Tdentry, fid->qid.path);
 	if(dbuf == nil){
 		respond(req, errstring[Ephase]);
 		return;
@@ -546,7 +546,7 @@
 		if(blkno == 0){
 			/* end reached, nothing found, create */
 			if(zblkno != 0){
-				cbuf = getbufchk(zblkno, 1,Bmod, Tdentry, Qpnone);
+				cbuf = getbufchk(zblkno, 1,Bwritable, Tdentry, Qpnone);
 				if(cbuf == nil){
 					putbuf(dbuf);
 					respond(req, errstring[Ephase]);
@@ -553,7 +553,7 @@
 					return;
 				}
 			}else{
-				cbuf = allocblocks(1, Bmod, Tdentry, fid->qid.path);
+				cbuf = allocblocks(1, Tdentry, fid->qid.path);
 				if(cbuf == nil){
 					putbuf(dbuf);
 					respond(req, errstring[Efull]);
@@ -622,7 +622,7 @@
 			respond(req, nil);
 			return;
 		}else{
-			cbuf = getbuf(blkno, 1,Breadonly);
+			cbuf = getbuf(blkno, 1,Breadonly, Bused);
 			if(cbuf == nil){
 				putbuf(dbuf);
 				respond(req, errstring[Ephase]);
@@ -1203,7 +1203,7 @@
 
 	if(d == nil || wbuf == nil || wbufsize == 0)
 		return 0;
-	buf = getdataspanat(d, offset/Maxspansize, Bmod);
+	buf = getdataspanat(d, offset/Maxspansize, Bwritable);
 	if(buf == nil)
 		return -1;
 	n = min(Maxspansize, wbufsize);
@@ -1239,7 +1239,7 @@
 				d->name, d->size, offset, offset/Maxspansize, s.blkno, s.len);
 	blkno = s.blkno;
 	nblocks = s.len;
-	buf = getbufchk(blkno, nblocks, Bmod, Tdata, d->qid.path);
+	buf = getbufchk(blkno, nblocks, Bwritable, Tdata, d->qid.path);
 	if(buf == nil)
 		return -1;
 
@@ -1274,7 +1274,7 @@
 		if((wbufsize+sizeof(Tag))%Rawblocksize > 0)
 			nblocks++;
 
-		buf = allocblocks(nblocks, Bmod, Tdata, d->qid.path);
+		buf = allocblocks(nblocks, Tdata, d->qid.path);
 		if(buf == nil)
 			return -1;
 		blkno = buf->blkno;
@@ -1307,7 +1307,7 @@
 		dprint("append update rel2abs d->name %s reli d->size %llud"
 				" d->size/Maxspansize %llud s.blkno %llud s.len %d\n",
 				d->name, d->size, d->size/Maxspansize, s.blkno, s.len);
-		buf = getbufchk(blkno, oldnblocks, Bmod, Tdata, d->qid.path);
+		buf = getbufchk(blkno, oldnblocks, Bwritable, Tdata, d->qid.path);
 		if(buf == nil)
 			return -1;
 
@@ -1318,7 +1318,7 @@
 			if((lastspansize+wbufsize+sizeof(Tag))%Rawblocksize > 0)
 				newnblocks++;
 		}
-		nbuf = allocblocks(newnblocks, Bmod, Tdata, d->qid.path);
+		nbuf = allocblocks(newnblocks, Tdata, d->qid.path);
 		if(nbuf == nil){
 			putbuf(buf);
 			return -1;
@@ -1381,7 +1381,7 @@
 	s32 n;
 	Iobuf *dbuf;
 
-	dbuf = getbufchk(dblkno, 1, Bmod, Tdentry, qpath);
+	dbuf = getbufchk(dblkno, 1, Bwritable, Tdentry, qpath);
 	if(dbuf == nil)
 		return 0;
 	d = &dbuf->io->d;
--- a/all.h
+++ b/all.h
@@ -78,7 +78,7 @@
 		u8	*xiobuf;	/* "real" buffer pointer */
 		Content *io;	/* cast'able to contents */
 	};
-	int	flags;
+	u8 readonly;
 };
 
 extern	u32	nbuckets;		/* n hash buckets for i/o */
@@ -88,8 +88,8 @@
 
 /* Iobuf routines - contents of the blocks in memory */
 int		checktag(Iobuf *p, u16 tag, u64 qpath);
-Iobuf*	getbuf(u64 blkno, u16 len, int flags);
-Iobuf*	getbufchk(u64 blkno, u16 len, int flags, int tag, u64 qpath);
+Iobuf*	getbuf(u64 blkno, u16 len, u8 readonly, u8 freshalloc);
+Iobuf*	getbufchk(u64 blkno, u16 len, u8 readonly, int tag, u64 qpath);
 void	iobufinit(void);
 void	putbuf(Iobuf *p);
 void	settag(Iobuf *p, int tag, u64 qpath);
@@ -96,7 +96,7 @@
 void	showbuf(Iobuf *p);
 
 /* routines to manipulate the contents */
-Iobuf*	allocblocks(u16 len, int flags, int tag, u64 qpath);
+Iobuf*	allocblocks(u16 len, int tag, u64 qpath);
 void	freeblockbuf(Iobuf *buf);
 void	freeblocks(u64 blkno, u16 len, u16 tag, u64 qpath);
 void	fsok(int ok);
--- a/config.c
+++ b/config.c
@@ -91,7 +91,7 @@
 	Iobuf *buf;
 	s32 n;
 
-	buf = getbufchk(bno, 1, Bmod, Tdata, Qpconfig);
+	buf = getbufchk(bno, 1, Bwritable, Tdata, Qpconfig);
 	if(buf == nil)
 		panic("cannot write config");
 	n = snprint((s8*)buf->io->buf, Blocksize,
--- a/dat.h
+++ b/dat.h
@@ -386,15 +386,13 @@
 
 /*
  * flags to getbuf
- * As all writes except for the Super block are synchronous, Bimm is mostly obsolete.
  */
 enum
 {
-	Breadonly = (1<<0),	/* read the block if miss */
-	Bprobe	= (1<<1),	/* return null if miss */
-	Bmod	= (1<<2),	/* set modified bit in buffer, needs writing */
-	Bimm	= (1<<3),	/* set immediate bit in buffer */
-	Bres	= (1<<4),	/* reserved, never renammed */
+	Breadonly	= 1,/* read the block, cannot write to it */
+	Bwritable	= 0,
+	Bfreshalloc = 1,/* newly allocated block, no contents on the disk */
+	Bused 		= 0,/* has contents on the disk */
 };
 
 /*
--- a/dentry.c
+++ b/dentry.c
@@ -76,13 +76,13 @@
 		return 0;
 	}
 	if(indblkno == 0){
-		if((buf = allocblocks(1, Bmod, tag, path)) == nil){
+		if((buf = allocblocks(1, tag, path)) == nil){
 			dprint("%s",errstring[Efull]);
 			return 0;
 		}
 		indblkno = buf->blkno;
 	}else{
-		if((buf = getbufchk(indblkno, 1, Bmod, tag, path)) == nil){
+		if((buf = getbufchk(indblkno, 1, Bwritable, tag, path)) == nil){
 			dprint("updateindblock: %s",errstring[Ephase]);
 			return 0;
 		}
@@ -180,7 +180,7 @@
 
 	if(iblkno == 0)
 		return;
-	ibuf = getbufchk(iblkno, 1, Bmod, tag, qpath);
+	ibuf = getbufchk(iblkno, 1, Bwritable, tag, qpath);
 	if(ibuf == nil)
 			dprint("%s",errstring[Ephase]);
 	if(tag == Tind0)
@@ -248,7 +248,7 @@
 
 	if(qpath < Qpusers || dblkno == 0)
 		return;
-	dbuf = getbufchk(dblkno, 1, Bmod, Tdentry, qpath);
+	dbuf = getbufchk(dblkno, 1, Bwritable, Tdentry, qpath);
 	if(dbuf == nil)
 			dprint("%s",errstring[Ephase]);
 
@@ -276,7 +276,7 @@
 
 	if(qpath < Qpusers || dblkno == 0)
 		return;
-	dbuf = getbufchk(dblkno, 1, Bmod, Tdentry, qpath);
+	dbuf = getbufchk(dblkno, 1, Bwritable, Tdentry, qpath);
 	if(dbuf == nil)
 			dprint("%s",errstring[Ephase]);
 	memcpy(&d, &dbuf->io->d, sizeof(Dentry));
@@ -311,7 +311,7 @@
 	if(qpath < Qpusers || dblkno == 0)
 		return;
 	/* clear the dentry to avoid links to removed content */
-	dbuf = getbufchk(dblkno, 1, Bmod, Tdentry, qpath);
+	dbuf = getbufchk(dblkno, 1, Bwritable, Tdentry, qpath);
 	if(dbuf == nil)
 			dprint("%s",errstring[Ephase]);
 	memcpy(&d, dbuf->io->buf, sizeof(Dentry));
@@ -329,7 +329,7 @@
 		if(s.blkno == 0)
 			break;
 
-		buf = getbuf(s.blkno, 1, Breadonly);
+		buf = getbuf(s.blkno, 1, Breadonly, Bused);
 		child = &buf->io->d;
 		cqpath = child->qid.path;
 		mode = child->mode;
@@ -443,7 +443,7 @@
 			dprint("searchdir reli %d s.blkno %llud s.len %d\n",
 					reli, s.blkno, s.len);
 
-		*buf = getbuf(s.blkno, s.len, Breadonly);
+		*buf = getbuf(s.blkno, s.len, Breadonly, Bused);
 		if(*buf == nil){
 			putbuf(*dbuf);
 			*dbuf = nil;
@@ -575,7 +575,7 @@
 	/* I cannot use truncatefile() below as it would
 		bfree() the blocks (which are already in the Extents frees)
 		and that would cause an inconsistency/panic */
-	dbuf = getbufchk(dblkno, 1, Bmod, Tdentry, Qpfrees);
+	dbuf = getbufchk(dblkno, 1, Bwritable, Tdentry, Qpfrees);
 	if(dbuf == nil)
 			dprint("%s",errstring[Ephase]);
 	d = &dbuf->io->d;
--- a/iobuf.c
+++ b/iobuf.c
@@ -55,7 +55,7 @@
 	Hiob.link->back is the oldest buffer that will be reused first.
  */
 Iobuf *
-getbuf(u64 blkno, u16 len, int flags)
+getbuf(u64 blkno, u16 len, u8 readonly, u8 freshalloc)
 {
 	Hiob *hp;
 	Iobuf *s, *p;
@@ -62,16 +62,16 @@
 	s8 ncollisions;
 
 	if(len == 0){
-		panic("getbuf len == 0 blkno %llud flags %d caller %#p\n",
-				blkno, flags, getcallerpc(&blkno));
+		panic("getbuf len == 0 blkno %llud readonly %d caller %#p\n",
+				blkno, readonly, getcallerpc(&blkno));
 	}
 	hp = &hiob[blkno%nbuckets];
 
 	if(chatty9p > 4)
 		dprint("getbuf blkno %llud len %d blkno%%nbuckets %llud pc 0x%p"
-				" hiob 0x%p hp 0x%p flags 0x%ux %d\n",
+				" hiob 0x%p hp 0x%p readonly %d\n",
 			blkno, len, blkno%nbuckets, getcallerpc(&blkno),
-			hiob, hp, flags, flags);
+			hiob, hp, readonly);
 	qlock(hp);
 	s = hp->link;
 	if(s == nil)
@@ -110,12 +110,13 @@
 				free(p->xiobuf);
 				p->xiobuf = emalloc9p(len*Rawblocksize);
 				p->len = len;
-				devread(blkno, p->xiobuf, len);
-				if((flags & Bmod) == 0){
+				if(freshalloc == 0)
+					devread(blkno, p->xiobuf, len);
+				if(readonly){
 					chkwunlock(p);
 					rlock(p);
 				}
-			}else if(flags & Bmod){
+			}else if(readonly == 0){
 				wlock(p);
 				if(chatty9p > 4)
 					dprint("	after wlock() blkno %llud\n", blkno);
@@ -126,7 +127,7 @@
 				rlock(p);
 			}
 			decref(p);
-			p->flags |= flags;
+			p->readonly = readonly;
 			return p;
 		}
 		p = p->fore;
@@ -133,11 +134,6 @@
 		if(p == s)
 			break;
 	}
-	/* TODO Is this still needed? would be helpful for read ahead */
-	/*if(flags == Bprobe){
-		qunlock(hp);
-		return nil;
-	}*/
 
 	/* maxed out our allowed number of collisions,
 		try to steal an older Iobuf without any ref's.
@@ -178,8 +174,9 @@
 	if(chatty9p > 4)
 		dprint("	after qunlock(hp) hp 0x%p blkno %llud\n",
 				hp, blkno);
-	devread(blkno, p->xiobuf, len);
-	if((flags & Bmod) == 0){
+	if(freshalloc == 0)
+		devread(blkno, p->xiobuf, len);
+	if(readonly){
 		if(chatty9p > 4)
 		dprint("new buffer: switching from wlock() to rlock() blkno %llud\n", blkno);
 		incref(p);
@@ -187,12 +184,12 @@
 		rlock(p);
 		decref(p);
 	}
-	p->flags = flags;
+	p->readonly = readonly;
 	return p;
 }
 
 Iobuf *
-getbufchk(u64 blkno, u16 len, int flags, int tag, u64 qpath)
+getbufchk(u64 blkno, u16 len, u8 readonly, int tag, u64 qpath)
 {
 	Iobuf *b;
 
@@ -199,11 +196,11 @@
 	if(chatty9p > 4)
 		dprint("getbufchk caller pc 0x%p\n", getcallerpc(&blkno));
 	if(len == 0){
-		panic("getbufchk len == 0 blkno %llud flags %d tag %d"
+		panic("getbufchk len == 0 blkno %llud readonly %d tag %d"
 				" qpath %llud caller %#p\n",
-				blkno, flags, tag, qpath, getcallerpc(&blkno));
+				blkno, readonly, tag, qpath, getcallerpc(&blkno));
 	}
-	b = getbuf(blkno, len, flags);
+	b = getbuf(blkno, len, readonly, Bused);
 	if(b != nil)
 		if(checktag(b, tag, qpath) == 0){
 			putbuf(b);
@@ -210,9 +207,9 @@
 			panic("checktag on %llud failed %s\n", blkno, errstring[Ephase]);
 		}
 	if(b->io == nil)
-		panic("b->io == nil blkno %llud flags %d tag %d"
+		panic("b->io == nil blkno %llud readonly %d tag %d"
 				" qpath %llud b->blkno %llud %d caller %#p\n",
-				blkno, flags, tag, qpath, b->blkno, b->len,
+				blkno, readonly, tag, qpath, b->blkno, b->len,
 				getcallerpc(&blkno));
 	return b;
 }
@@ -224,10 +221,10 @@
 
 	if(chatty9p > 4)
 		dprint("putbuf p->blkno 0x%d t->c->type %d devtab[t->c->type]->dc %c\n"
-				"	p 0x%p p->flags 0x%ux %d\n"
+				"	p 0x%p p->readonly %d\n"
 				"	p->xiobuf 0x%p",
 				p->blkno, t->c->type, devtab[t->c->type]->dc,
-				p, p->flags, p->flags, p->xiobuf);
+				p, p->readonly, p->xiobuf);
 
  */
 void
@@ -246,7 +243,7 @@
 		return;
 	}
 
-	buf = getbufchk(bno, 1, Bmod, Tdata, qpath);
+	buf = getbufchk(bno, 1, Bwritable, Tdata, qpath);
 	if(buf == nil){
 		panic("bkp: buf == nil\n");
 	}
@@ -283,7 +280,13 @@
 	
 	if(chatty9p > 4)
 		dprint("putbuf p->blkno %llud\n", p->blkno);
-	if(p->flags & Bmod){
+
+	if(p->readonly){
+		p->readonly = 0;
+		chkrunlock(p);
+		if(chatty9p > 4)
+		dprint(" .. runlock()'ed\n");
+	}else{
 		if(canwlock(p)){
 			panic("putbuf: buffer not locked %llud\n", p->blkno);
 		}
@@ -305,18 +308,9 @@
 			for(i=0; i<Nbkp; i++)
 				bkp(p, config.root.dest[i], Qproot0+i*3);
 		}
-	}
-
-	if(p->flags & Bmod){
-		p->flags = 0;
 		chkwunlock(p);
 		if(chatty9p > 4)
 		dprint(" .. wunlock()'ed\n");
-	}else{
-		p->flags = 0;
-		chkrunlock(p);
-		if(chatty9p > 4)
-		dprint(" .. runlock()'ed\n");
 	}
 }
 
@@ -353,8 +347,8 @@
 void
 settag(Iobuf *p, int tagtype, u64 qpath)
 {
-	if((p->flags & Bmod) == 0)
-		panic("settag %s(%llux) tag/path=%s/%llud: not Bmod\n",
+	if(p->readonly)
+		panic("settag %s(%llux) tag/path=%s/%llud: not Bwritable\n",
 				devfile, (u64)p->blkno, tagnames[tagtype], qpath);
 	if(p->io == nil)
 		panic("settag %s(%llux) tag/path=%s/%llud: p->io == nil\n",
@@ -412,11 +406,11 @@
 	}
 	dprint("showbuf p 0x%p blkno %llud len %d\n"
 			"	fore 0x%p back 0x%p\n"
-			"	xiobuf 0x%p flags 0x%x\n"
+			"	xiobuf 0x%p readonly 0x%x\n"
 			"	caller %#p\n",
 			p, p->blkno, p->len,
 			p->fore, p->back,
-			p->xiobuf, p->flags,
+			p->xiobuf, p->readonly,
 			getcallerpc(&p));
 	if(p->io != nil)
 		showblock((u8*)p->io);
--- a/mafs.c
+++ b/mafs.c
@@ -42,7 +42,7 @@
 	static char *nets[8];
 	int doream, stdio, netc;
 	char buf[Namelen];
-	int pid, ctl, n;
+	int pid, ctl;
 
 	progname = "mafs";
 	procname = "init";
--- a/sub.c
+++ b/sub.c
@@ -36,7 +36,7 @@
 	Iobuf *sb;
 	Superb *s;
 
-	sb = getbufchk(Bsuper, 1, Bmod, Tdata, Qpsuper);
+	sb = getbufchk(Bsuper, 1, Bwritable, Tdata, Qpsuper);
 	if(sb == nil){
 		panic("newqpath: sb == nil\n");
 	}
@@ -93,8 +93,9 @@
 		return 0;
 }
 
+/* making the assumption that all allocations are not readonly */
 Iobuf *
-allocblocks(u16 len, int flags, int tag, u64 qpath)
+allocblocks(u16 len, int tag, u64 qpath)
 {
 	u64 blkno;
 	Iobuf *buf;
@@ -106,7 +107,7 @@
 	if(chatty9p > 1)
 		dprint("alloc %d at %llud\n", len, blkno);
 	/* cannot do getbufchk() unless we ream the whole disk at start */
-	buf = getbuf(blkno, len, flags|Bmod);
+	buf = getbuf(blkno, len, Bwritable, Bfreshalloc);
 	/* clear the buf to avoid leaks on reuse */
 	memset(buf->io, 0, buf->len*Rawblocksize);
 	settag(buf, tag, qpath);
@@ -117,8 +118,8 @@
 void
 freeblockbuf(Iobuf *buf)
 {
-	if((buf->flags&Bmod) == 0)
-		panic("freeblockbuf without Bmod");
+	if(buf->readonly)
+		panic("freeblockbuf without Bwritable");
 
 	/* clear the buf to avoid leaks on reuse */
 	memset(buf->io, 0, buf->len*Rawblocksize);
@@ -137,7 +138,7 @@
 				 blkno, config.nblocks);
 		return;
 	}
-	buf = getbufchk(blkno, len, Bmod, tag, qpath);
+	buf = getbufchk(blkno, len, Bwritable, tag, qpath);
 	if(buf == nil)
 		dprint("%s",errstring[Ephase]);
 	freeblockbuf(buf);
@@ -168,7 +169,7 @@
 	Iobuf *sb;
 	Superb *s;
 
-	sb = getbufchk(Bsuper, 1, Bmod, Tdata, Qpsuper);
+	sb = getbufchk(Bsuper, 1, Bwritable, Tdata, Qpsuper);
 	if(sb == nil){
 		panic("newqpath: sb == nil\n");
 	}
@@ -203,7 +204,7 @@
 {
 	Iobuf *b;
 
-	b = getbuf(bno, len, Bmod);
+	b = getbuf(bno, len, Bwritable, Bfreshalloc);
 	memset(b->io, 0, len*Rawblocksize);
 	settag(b, Tdata, qpath);
 	putbuf(b);
@@ -216,7 +217,7 @@
 	Dentry *d;
 	u16 len;
 
-	b = getbuf(dblkno, 1, Bmod);
+	b = getbuf(dblkno, 1, Bwritable, Bfreshalloc);
 	memset(b->io, 0, Rawblocksize);
 	settag(b, Tdentry, qpath);
 	d = &b->io->d;
@@ -260,7 +261,7 @@
 					"10006:glenda:glenda:\n"
 					"10007:manies::\n";
 
-	b = getbuf(Badm, 1, Bmod);
+	b = getbuf(Badm, 1, Bwritable, Bfreshalloc);
 	memset(b->io, 0, Rawblocksize);
 	settag(b, Tdentry, Qpadm);
 	d = &b->io->d;
@@ -283,7 +284,7 @@
 	d->dspans[5] = (Spanid){Bdfrees, 1};
 	putbuf(b);
 
-	b = getbuf(Bbkp, 1, Bmod);
+	b = getbuf(Bbkp, 1, Bwritable, Bfreshalloc);
 	memset(b->io, 0, Rawblocksize);
 	settag(b, Tdentry, Qpbkp);
 	d = &b->io->d;
@@ -328,7 +329,7 @@
 	reamfile(Bdroot1, Qproot1, "root.1",
 			 Blocksize, Bbkp, Qpbkp, config.root.dest[1]);
 
-	b = getbuf(Busers, 1, Bmod);
+	b = getbuf(Busers, 1, Bwritable, Bfreshalloc);
 	if(b == nil)
 		panic("cannot get Busers");
 	memset(b->io, 0, Rawblocksize);
@@ -343,7 +344,7 @@
 	Iobuf *b;
 	Dentry *d;
 
-	b = getbuf(Broot, 1, Bmod);
+	b = getbuf(Broot, 1, Bwritable, Bfreshalloc);
 	if(b == nil)
 		panic("rootream b == nil");
 	memset(b->io, 0, Rawblocksize);
@@ -372,7 +373,7 @@
 	u64 i, nbused;
 
 	nbused = Nbused;
-	sbuf = getbuf(Bsuper, 1, Bmod);
+	sbuf = getbuf(Bsuper, 1, Bwritable, Bfreshalloc);
 	if(sbuf == nil)
 		panic("superream: sbuf == nil");
 	memset(sbuf->io, 0, Rawblocksize);
@@ -454,7 +455,7 @@
 
 	superream(size, nblocks);
 
-	iob = getbuf(Bmagicb, 1, Bmod);
+	iob = getbuf(Bmagicb, 1, Bwritable, Bfreshalloc);
 	snprint((s8*)iob->io->buf+256, Rawblocksize-256, "%s%d\n%llud\n",
 			magic, Rawblocksize, Maxspansize);
 	settag(iob, Tmagic, Qpmagic);