shithub: riscv

Download patch

ref: efddf485006559bef5e8791b637e742d865df434
parent: c8d9390070a5c3585e064ee25a64c49e15618b99
author: cinap_lenrek <[email protected]>
date: Wed Mar 15 15:30:33 EDT 2017

upas/fs: getting rid of the fine-grain locking...

now that locking works, its time to get rid of it.
given that the only concurrency is between the periodic reader
process and the filesystem code, just make sure the filesystem
rpc handlers do not run concurrenty while syncing is in progress
with a single qlock.

--- a/sys/src/cmd/upas/fs/dat.h
+++ b/sys/src/cmd/upas/fs/dat.h
@@ -147,7 +147,6 @@
 
 typedef struct Mailbox Mailbox;
 struct Mailbox {
-	QLock;
 	int	refs;
 	Mailbox	*next;
 	int	id;
@@ -355,7 +354,6 @@
 extern ulong	msgfreed;
 extern Mailbox	*mbl;
 extern Message	*root;
-extern QLock	mbllock;
 extern Refs	*rtab;
 
 #define	dprint(...)	if(debug) fprint(2, __VA_ARGS__); else {}
--- a/sys/src/cmd/upas/fs/fs.c
+++ b/sys/src/cmd/upas/fs/fs.c
@@ -109,7 +109,6 @@
 	Hsize=	1999,
 };
 
-
 char	*mntpt;
 char	user[Elemlen];
 int	Dflag;
@@ -121,7 +120,6 @@
 int	plumbing = 1;
 ulong	cachetarg = Maxcache;
 Mailbox	*mbl;
-QLock	mbllock;
 
 static	int	messagesize = 8*1024 + IOHDRSZ;
 static	int	mfd[2];
@@ -133,6 +131,7 @@
 static	Fcall	rhdr;
 static	Fcall	thdr;
 static	Fid	*fids;
+static QLock	synclock;
 
 void
 sanemsg(Message *m)
@@ -778,17 +777,13 @@
 	nf->busy = 1;
 	nf->open = 0;
 	nf->mb = f->mb;
-	if(nf->mb){
-		qlock(nf->mb);
+	if(nf->mb)
 		mboxincref(nf->mb);
-	}
 	if(nf->m = f->m)
 		msgincref(gettopmsg(nf->mb, nf->m));
 	if(nf->mtop = f->mtop)
 		msgincref(nf->mtop);
 	nf->qid = f->qid;
-	if(nf->mb)
-		qunlock(nf->mb);
 	return nf;
 }
 
@@ -816,10 +811,6 @@
 	t = FILE(f->qid.path);
 	rv = Enotexist;
 
-	qlock(&mbllock);
-	if(f->mb)
-		qlock(f->mb);
-
 	/* this must catch everything except . and .. */
 retry:
 	t1 = FILE(f->qid.path);
@@ -833,12 +824,8 @@
 	if(h != nil){
 		if(f->m)
 			msgdecref(f->mb, gettopmsg(f->mb, f->m));
-		if(f->mb && f->mb != h->mb){
-			qunlock(f->mb);
+		if(f->mb && f->mb != h->mb)
 			mboxdecref(f->mb);
-		}
-		if(h->mb && h->mb != f->mb)
-			qlock(h->mb);
 		f->mb = h->mb;
 		f->m = h->m;
 		if(f->m)
@@ -864,10 +851,6 @@
 		goto retry;
 	}
 
-	if(f->mb)
-		qunlock(f->mb);
-	qunlock(&mbllock);
-
 	if(rv == nil)
 		return rv;
 
@@ -890,12 +873,9 @@
 			f->qid.vers = 0;
 			mb = f->mb;
 			f->mb = nil;
-			qlock(&mbllock);
 			mboxdecref(mb);
-			qunlock(&mbllock);
 			break;
 		case Qdir:
-			qlock(f->mb);
 			if(Topmsg(f->mb, f->m)){
 				f->qid.path = PATH(f->mb->id, Qmbox);
 				f->qid.type = QTDIR;
@@ -909,7 +889,6 @@
 				f->qid.path = PATH(f->m->id, Qdir);
 				f->qid.type = QTDIR;
 			}
-			qunlock(f->mb);
 			break;
 		}
 		rv = nil;
@@ -975,12 +954,10 @@
 
 	/* make sure we've decoded */
 	if(file == Qbody){
-		qlock(f->mb);
 		cachebody(f->mb, f->m);
 		decode(f->m);
 		convert(f->m);
 		putcache(f->mb, f->m);
-		qunlock(f->mb);
 	}
 
 	rhdr.iounit = 0;
@@ -1003,8 +980,6 @@
 	long pos;
 	Mailbox *mb;
 
-	qlock(&mbllock);
-
 	n = 0;
 	pos = 0;
 	mkstat(&d, nil, nil, Qctl);
@@ -1011,7 +986,7 @@
 	m = convD2M(&d, &buf[n], blen);
 	if(off <= pos){
 		if(m <= BIT16SZ || m > cnt)
-			goto out;
+			return n;
 		n += m;
 		cnt -= m;
 	}
@@ -1018,9 +993,7 @@
 	pos += m;
 		
 	for(mb = mbl; mb != nil; mb = mb->next){
-		qlock(mb);
 		mkstat(&d, mb, nil, Qmbox);
-		qunlock(mb);
 		m = convD2M(&d, &buf[n], blen - n);
 		if(off <= pos){
 			if(m <= BIT16SZ || m > cnt)
@@ -1030,8 +1003,6 @@
 		}
 		pos += m;
 	}
-out:
-	qlock(&mbllock);
 	return n;
 }
 
@@ -1043,7 +1014,6 @@
 	long pos;
 	Message *msg;
 
-	qlock(f->mb);
 	if(off == 0)
 		syncmbox(f->mb, 1);
 
@@ -1054,7 +1024,7 @@
 		if(off == 0){
 			if(m <= BIT16SZ || m > cnt){
 				f->fptr = nil;
-				goto out;
+				return n;
 			}
 			n += m;
 			cnt -= m;
@@ -1091,8 +1061,6 @@
 	f->foff = pos;
 	f->fptr = msg;
 	f->fvers = f->mb->vers;
-out:
-	qunlock(f->mb);
 	return n;
 }
 
@@ -1104,7 +1072,6 @@
 	long pos;
 	Message *msg;
 
-	qlock(f->mb);
 	n = 0;
 	pos = 0;
 	for(i = 0; i < Qmax; i++){
@@ -1112,7 +1079,7 @@
 		m = convD2M(&d, &buf[n], blen - n);
 		if(off <= pos){
 			if(m <= BIT16SZ || m > cnt)
-				goto out;
+				return n;
 			n += m;
 			cnt -= m;
 		}
@@ -1129,8 +1096,6 @@
 		}
 		pos += m;
 	}
-out:
-	qunlock(f->mb);
 	return n;
 }
 
@@ -1171,7 +1136,6 @@
 		return nil;
 	}
 
-	qlock(f->mb);
 	switch(t){
 	case Qctl:
 		rhdr.count = 0;
@@ -1214,7 +1178,6 @@
 		}
 		break;
 	}
-	qunlock(f->mb);
 	return nil;
 }
 
@@ -1335,10 +1298,7 @@
 			argc = tokenize(thdr.data, argv, nelem(argvbuf));
 			if(argc == 0)
 				return Ebadctl;
-			qlock(f->mb);
-			err = f->mb->ctl(f->mb, argc, argv);
-			qunlock(f->mb);
-			return err;
+			return f->mb->ctl(f->mb, argc, argv);
 		}
 		break;
 	case Qflags:
@@ -1347,11 +1307,8 @@
 		 */
 		if(!f->mb || !f->m)
 			break;
-		qlock(f->mb);
 		m = gettopmsg(f->mb, f->m);
-		err = modflags(f->mb, m, thdr.data);
-		qunlock(f->mb);
-		return err;
+		return modflags(f->mb, m, thdr.data);
 	}
 	return Eperm;
 }
@@ -1366,8 +1323,6 @@
 	f->fid = -1;
 	f->open = 0;
 	mb = f->mb;
-	if(mb)
-		qlock(mb);
 	if(f->mtop)
 		msgdecref(mb, f->mtop);
 	if(f->m)
@@ -1375,10 +1330,7 @@
 	f->m = f->mtop = nil;
 	if(mb){
 		f->mb = nil;
-		qunlock(mb);
-		qlock(&mbllock);
 		mboxdecref(mb);
-		qunlock(&mbllock);
 	}
 	f->busy = 0;
 	return 0;
@@ -1388,11 +1340,9 @@
 rremove(Fid *f)
 {
 	if(f->m != nil){
-		qlock(f->mb);
 		if(!f->m->deleted)
 			mailplumb(f->mb, f->m, 1);
 		f->m->deleted = Deleted;
-		qunlock(f->mb);
 	}
 	return rclunk(f);
 }
@@ -1402,15 +1352,11 @@
 {
 	Dir d;
 
-	if(f->mb)
-		qlock(f->mb);
 	if(FILE(f->qid.path) == Qmbox)
 		syncmbox(f->mb, 1);
 	mkstat(&d, f->mb, f->m, FILE(f->qid.path));
 	rhdr.nstat = convD2M(&d, mbuf, messagesize - IOHDRSZ);
 	rhdr.stat = mbuf;
-	if(f->mb)
-		qunlock(f->mb);
 	return 0;
 }
 
@@ -1473,6 +1419,7 @@
 		if(Dflag)
 			fprint(2, "%s:<-%F\n", argv0, &thdr);
 
+		qlock(&synclock);
 		rhdr.data = (char*)mdata + messagesize;
 		if(!fcalls[thdr.type])
 			err = "bad fcall type";
@@ -1486,6 +1433,8 @@
 			rhdr.fid = thdr.fid;
 		}
 		rhdr.tag = thdr.tag;
+		qunlock(&synclock);
+
 		if(Dflag)
 			fprint(2, "%s:->%F\n", argv0, &rhdr);
 		n = convS2M(&rhdr, mdata, messagesize);
@@ -1506,11 +1455,9 @@
 	setname(readerargv);
 	sleep(15*1000);
 	for(;;){
+		qlock(&synclock);
 		t = time(0);
-		qlock(&mbllock);
 		for(mb = mbl; mb != nil; mb = mb->next){
-			if(!canqlock(mb))
-				continue;
 			if(mb->waketime != 0 && t >= mb->waketime){
 				mb->waketime = 0;
 				break;
@@ -1526,14 +1473,14 @@
 					free(d);
 				}
 			}
-			qunlock(mb);
 		}
-		qunlock(&mbllock);
-		if(mb != nil){
+		if(mb != nil) {
 			syncmbox(mb, 1);
-			qunlock(mb);
-		} else
+			qunlock(&synclock);
+		} else {
+			qunlock(&synclock);
 			sleep(15*1000);
+		}
 	}
 }
 
@@ -1662,8 +1609,6 @@
 	return to - buf;
 }
 
-QLock hashlock;
-
 uint
 hash(ulong ppath, char *name)
 {
@@ -1684,14 +1629,10 @@
 	int h;
 	Hash *hp;
 
-	qlock(&hashlock);
 	h = hash(ppath, name);
 	for(hp = htab[h]; hp != nil; hp = hp->next)
-		if(ppath == hp->ppath && strcmp(name, hp->name) == 0){
-			qunlock(&hashlock);
+		if(ppath == hp->ppath && strcmp(name, hp->name) == 0)
 			return hp;
-		}
-	qunlock(&hashlock);
 	return nil;
 }
 
@@ -1701,7 +1642,6 @@
 	int h;
 	Hash *hp, **l;
 
-	qlock(&hashlock);
 	h = hash(ppath, name);
 	for(l = &htab[h]; *l != nil; l = &(*l)->next){
 		hp = *l;
@@ -1709,11 +1649,9 @@
 			hp->m = m;
 			hp->mb = mb;
 			hp->qid = qid;
-			qunlock(&hashlock);
 			return;
 		}
 	}
-
 	*l = hp = emalloc(sizeof(*hp));
 	hp->m = m;
 	hp->mb = mb;
@@ -1720,7 +1658,6 @@
 	hp->qid = qid;
 	hp->name = name;
 	hp->ppath = ppath;
-	qunlock(&hashlock);
 }
 
 void
@@ -1729,7 +1666,6 @@
 	int h;
 	Hash *hp, **l;
 
-	qlock(&hashlock);
 	h = hash(ppath, name);
 	for(l = &htab[h]; *l != nil; l = &(*l)->next){
 		hp = *l;
@@ -1740,7 +1676,6 @@
 			break;
 		}
 	}
-	qunlock(&hashlock);
 }
 
 void
--- a/sys/src/cmd/upas/fs/mbox.c
+++ b/sys/src/cmd/upas/fs/mbox.c
@@ -65,7 +65,6 @@
 	int n, d, y, a;
 	Message *m, *next;
 
-	assert(!canqlock(mb));
 	a = mb->root->subname;
 	if(rdidxfile(mb, doplumb) == -2)
 		wridxfile(mb);
@@ -146,9 +145,6 @@
 	henter(PATH(0, Qtop), mb->name,
 		(Qid){PATH(mb->id, Qmbox), mb->vers, QTDIR}, nil, mb);
 done:
-	if(mb == nil)
-		return err;
-	qunlock(mb);
 	return err;
 }
 
@@ -206,7 +202,6 @@
 	}
 
 	/* make sure name isn't taken */
-	qlock(&mbllock);
 	for(l = &mbl; *l != nil; l = &(*l)->next)
 		if(strcmp((*l)->name, mb->name) == 0){
 			if(strcmp(path, (*l)->path) == 0)
@@ -216,7 +211,6 @@
 			if(mb->close)
 				mb->close(mb);
 			free(mb);
-			qunlock(&mbllock);
 			return rv;
 		}
 
@@ -229,9 +223,7 @@
 	mb->mtree = avlcreate(mtreecmp);
 
 	*l = mb;
-	qunlock(&mbllock);
 
-	qlock(mb);
 	henter(PATH(0, Qtop), mb->name,
 		(Qid){PATH(mb->id, Qmbox), mb->vers, QTDIR}, nil, mb);
 	if(mb->ctl)
@@ -240,8 +232,6 @@
 	rv = syncmbox(mb, 0);
 	if(r)
 		*r = mb;
-	else
-		qunlock(mb);
 
 	return rv;
 }
@@ -252,7 +242,6 @@
 {
 	Mailbox **l, *mb;
 
-	qlock(&mbllock);
 	for(l=&mbl; *l != nil; l=&(*l)->next)
 		if(strcmp(name, (*l)->name) == 0){
 			mb = *l;
@@ -261,7 +250,6 @@
 			break;
 		}
 	hfree(PATH(0, Qtop), name);
-	qunlock(&mbllock);
 }
 
 void
@@ -270,14 +258,9 @@
 	char *err;
 	Mailbox *m;
 
-	qlock(&mbllock);
-	for(m = mbl; m != nil; m = m->next){
-		qlock(m);
+	for(m = mbl; m != nil; m = m->next)
 		if(err = syncmbox(m, 1))
 			eprint("syncmbox: %s\n", err);
-		qunlock(m);
-	}
-	qunlock(&mbllock);
 }
 
 
@@ -288,7 +271,6 @@
 	Mailbox **l, *mb;
 
 	found = 0;
-	qlock(&mbllock);
 	for(l=&mbl; *l != nil; l=&(*l)->next)
 		if(strcmp(name, (*l)->path) == 0){
 			mb = *l;
@@ -300,7 +282,6 @@
 			break;
 		}
 	hfree(PATH(0, Qtop), name);
-	qunlock(&mbllock);
 
 	if(found == 0)
 		return "maibox not found";
@@ -1065,13 +1046,9 @@
 	Mailbox *mb;
 	Message *m;
 
-	qlock(&mbllock);
 	for(mb = mbl; mb != nil; mb = mb->next)
-		if(strcmp(av[0], mb->name) == 0){
-			qlock(mb);
+		if(strcmp(av[0], mb->name) == 0)
 			break;
-		}
-	qunlock(&mbllock);
 	if(mb == nil)
 		return "no such mailbox";
 
@@ -1089,7 +1066,6 @@
 			}
 	if(needwrite)
 		syncmbox(mb, 1);
-	qunlock(mb);
 	return 0;
 }
 
@@ -1103,13 +1079,9 @@
 
 	if(argc%2)
 		return "bad flags";
-	qlock(&mbllock);
 	for(mb = mbl; mb; mb = mb->next)
-		if(strcmp(*argv, mb->name) == 0){
-			qlock(mb);
+		if(strcmp(*argv, mb->name) == 0)
 			break;
-		}
-	qunlock(&mbllock);
 	if(mb == nil)
 		return "no such mailbox";
 	needwrite = 0;
@@ -1124,13 +1096,9 @@
 			}
 	if(needwrite)
 		syncmbox(mb, 1);
-	qunlock(mb);
 	return rerr;
 }
 
-/*
- *  the following are called with the mailbox qlocked
- */
 void
 msgincref(Message *m)
 {
@@ -1140,7 +1108,6 @@
 void
 msgdecref(Mailbox *mb, Message *m)
 {
-	assert(!canqlock(mb));
 	assert(m->refs > 0);
 	m->refs--;
 	if(m->refs == 0){
@@ -1151,9 +1118,6 @@
 	}
 }
 
-/*
- *  the following are called with mbllock'd
- */
 void
 mboxincref(Mailbox *mb)
 {
@@ -1177,7 +1141,6 @@
 void
 mboxdecref(Mailbox *mb)
 {
-	qlock(mb);
 	assert(mb->refs > 0);
 	mb->refs--;
 	if(mb->refs == 0){
@@ -1193,8 +1156,7 @@
 		free(mb->mtree);
 		free(mb->d);
 		free(mb);
-	} else
-		qunlock(mb);
+	}
 }