plan 9 kernel history: overview | file list | diff list

2000/0105/port/devmnt.c (diff list | history)

port/devmnt.c on 1990/0227
1990/0227    
#include	"u.h" 
1992/0321    
#include	"../port/lib.h" 
1990/0227    
#include	"mem.h" 
#include	"dat.h" 
#include	"fns.h" 
1992/0111    
#include	"../port/error.h" 
1990/0227    
 
1991/0911    
struct Mntrpc 
1990/0227    
{ 
1995/0107    
	Chan*	c;		/* Channel for whom we are working */ 
	Mntrpc*	list;		/* Free/pending list */ 
1991/0911    
	Fcall	request;	/* Outgoing file system protocol message */ 
	Fcall	reply;		/* Incoming reply */ 
1995/0107    
	Mnt*	m;		/* Mount device during rpc */ 
1991/0911    
	Rendez	r;		/* Place to hang out */ 
1995/0107    
	char*	rpc;		/* I/O Data buffer */ 
1991/0911    
	char	done;		/* Rpc completed */ 
1998/0917    
	uvlong	stime;		/* start time for mnt statistics */ 
	ulong	reqlen;		/* request length for mnt statistics */ 
	ulong	replen;		/* reply length for mnt statistics */ 
2000/0105    
	Mntrpc*	flushed;	/* message this one flushes */ 
1990/0227    
}; 
 
1991/0911    
struct Mntalloc 
1990/0227    
{ 
	Lock; 
1995/0107    
	Mnt*	list;		/* Mount devices in use */ 
	Mnt*	mntfree;	/* Free list */ 
	Mntrpc*	rpcfree; 
1999/0212    
	int	nrpcfree; 
	int	nrpcused; 
1994/1124    
	ulong	id; 
1992/0620    
	int	rpctag; 
1991/0911    
}mntalloc; 
1990/0227    
 
1993/0501    
void	mattach(Mnt*, Chan*, char*); 
1997/0327    
void	mntauth(Mnt*, Mntrpc*, char*, ushort); 
1992/0825    
Mnt*	mntchk(Chan*); 
1991/0911    
void	mntdirfix(uchar*, Chan*); 
2000/0105    
Mntrpc*	mntflushalloc(Mntrpc*); 
void	mntflushfree(Mnt*, Mntrpc*); 
1992/0825    
void	mntfree(Mntrpc*); 
void	mntgate(Mnt*); 
1992/0320    
void	mntpntfree(Mnt*); 
1992/0825    
void	mntqrm(Mnt*, Mntrpc*); 
1993/0501    
Mntrpc*	mntralloc(Chan*); 
1998/0327    
long	mntrdwr(int, Chan*, void*, long, vlong); 
long	mnt9prdwr(int, Chan*, void*, long, vlong); 
1992/0825    
void	mntrpcread(Mnt*, Mntrpc*); 
void	mountio(Mnt*, Mntrpc*); 
void	mountmux(Mnt*, Mntrpc*); 
void	mountrpc(Mnt*, Mntrpc*); 
1999/0320    
int	rpcattn(void*); 
1993/0501    
void	mclose(Mnt*, Chan*); 
Chan*	mntchan(void); 
1990/0227    
 
1998/0917    
void (*mntstats)(int, Chan*, uvlong, ulong); 
1994/0513    
 
1991/0911    
enum 
1990/0303    
{ 
1993/1011    
	Tagspace	= 1, 
1991/0911    
}; 
1990/0604    
 
1997/0327    
static void 
1991/0911    
mntreset(void) 
1990/0227    
{ 
1991/0911    
	mntalloc.id = 1; 
1992/0620    
	mntalloc.rpctag = Tagspace; 
1993/1015    
 
	cinit(); 
1990/0604    
} 
 
1997/0327    
static Chan* 
1991/0911    
mntattach(char *muxattach) 
1990/0227    
{ 
1992/0620    
	Mnt *m; 
1993/0321    
	Chan *c, *mc; 
1993/0501    
	char buf[NAMELEN]; 
1990/0227    
	struct bogus{ 
		Chan	*chan; 
		char	*spec; 
1993/1015    
		int	flags; 
1990/0227    
	}bogus; 
 
1991/0911    
	bogus = *((struct bogus *)muxattach); 
1992/0620    
	c = bogus.chan; 
 
	lock(&mntalloc); 
	for(m = mntalloc.list; m; m = m->list) { 
1993/1016    
		if(m->c == c && m->id) { 
1991/0911    
			lock(m); 
1992/0620    
			if(m->id && m->ref > 0 && m->c == c) { 
1991/0911    
				m->ref++; 
				unlock(m); 
1997/1205    
				unlock(&mntalloc); 
1993/0501    
				c = mntchan(); 
				if(waserror()) { 
					chanfree(c); 
					nexterror(); 
				} 
				mattach(m, c, bogus.spec); 
				poperror(); 
1993/1016    
				if(bogus.flags&MCACHE) 
					c->flag |= CCACHE; 
1993/0501    
				return c; 
1991/0911    
			} 
1998/0512    
			unlock(m); 
1991/0911    
		} 
1991/0901    
	} 
1993/1015    
 
1991/0911    
	m = mntalloc.mntfree; 
1992/0620    
	if(m != 0) 
1998/0512    
		mntalloc.mntfree = m->list; 
1992/0620    
	else { 
		m = malloc(sizeof(Mnt)); 
		if(m == 0) { 
			unlock(&mntalloc); 
			exhausted("mount devices"); 
		} 
	} 
	m->list = mntalloc.list; 
	mntalloc.list = m; 
1991/0911    
	m->id = mntalloc.id++; 
	unlock(&mntalloc); 
1992/0620    
 
1997/1205    
	lock(m); 
1991/0904    
	m->ref = 1; 
1991/0911    
	m->queue = 0; 
	m->rip = 0; 
1992/0620    
	m->c = c; 
1991/1011    
	m->c->flag |= CMSG; 
1995/0508    
	if(strncmp(bogus.spec, "mntblk=", 7) == 0) { 
		m->blocksize = strtoul(bogus.spec+7, 0, 0); 
		if(m->blocksize > MAXFDATA) 
			m->blocksize = MAXFDATA; 
		print("mount blk %d\n", m->blocksize); 
1994/0831    
		bogus.spec = ""; 
	} 
	else 
1999/1109    
		m->blocksize = MAXFDATA; 
1993/1016    
	m->flags = bogus.flags & ~MCACHE; 
1991/0911    
 
	incref(m->c); 
1993/0501    
 
1998/0825    
	sprint(buf, "#M%lud", m->id); 
1993/0501    
 
1990/0227    
	unlock(m); 
1990/0604    
 
1993/0501    
	c = mntchan(); 
1992/0320    
	if(waserror()) { 
1993/0501    
		mclose(m, c); 
		/* Close must not be called since it will 
		 * call mnt recursively 
		 */ 
		chanfree(c); 
1992/0320    
		nexterror(); 
	} 
 
1993/0501    
	mattach(m, c, bogus.spec); 
	poperror(); 
1993/0321    
 
1993/0808    
	/* 
	 * Detect a recursive mount for a mount point served by exportfs. 
1995/0202    
	 * If CHDIR is clear in the returned qid, the foreign server is 
1993/0808    
	 * requesting the mount point be folded into the connection 
	 * to the exportfs. In this case the remote mount driver does 
	 * the multiplexing. 
	 */ 
	mc = m->c; 
1993/0323    
	if(mc->type == devno('M', 0) && (c->qid.path&CHDIR) == 0) { 
1993/0501    
		mclose(m, c); 
1993/0323    
		c->qid.path |= CHDIR; 
1993/0321    
		c->mntptr = mc->mntptr; 
1993/0501    
		c->mchan = c->mntptr->c; 
1993/0321    
		c->mqid = c->qid; 
		incref(c->mntptr); 
	} 
 
1993/1016    
	if(bogus.flags & MCACHE) 
		c->flag |= CCACHE; 
 
1992/0320    
	return c; 
1991/0911    
} 
1990/0604    
 
1993/0501    
Chan* 
mntchan(void) 
1991/0911    
{ 
	Chan *c; 
 
1993/0501    
	c = devattach('M', 0); 
1991/0918    
	lock(&mntalloc); 
	c->dev = mntalloc.id++; 
	unlock(&mntalloc); 
1993/0501    
 
	return c; 
} 
 
void 
mattach(Mnt *m, Chan *c, char *spec) 
{ 
	ulong id; 
	Mntrpc *r; 
 
	r = mntralloc(0); 
1992/0620    
	c->mntptr = m; 
1991/0911    
 
1994/1212    
	if(waserror()) { 
1992/0314    
		mntfree(r); 
		nexterror(); 
	} 
 
1991/0911    
	r->request.type = Tattach; 
	r->request.fid = c->fid; 
1993/0501    
	memmove(r->request.uname, up->user, NAMELEN); 
1991/0911    
	strncpy(r->request.aname, spec, NAMELEN); 
1993/0330    
	id = authrequest(m->c->session, &r->request); 
1991/0911    
	mountrpc(m, r); 
1993/0330    
	authreply(m->c->session, id, &r->reply); 
1991/0911    
 
	c->qid = r->reply.qid; 
	c->mchan = m->c; 
1990/0303    
	c->mqid = c->qid; 
1993/0501    
 
1990/0227    
	poperror(); 
1991/0911    
	mntfree(r); 
1992/0318    
} 
 
1997/0327    
static Chan* 
1990/0227    
mntclone(Chan *c, Chan *nc) 
{ 
	Mnt *m; 
1991/0911    
	Mntrpc *r; 
	int alloc = 0; 
1990/0227    
 
1991/0911    
	m = mntchk(c); 
1993/0501    
	r = mntralloc(c); 
1991/0911    
	if(nc == 0) { 
1990/0227    
		nc = newchan(); 
1991/0911    
		alloc = 1; 
1990/0227    
	} 
1992/0824    
	if(waserror()) { 
1991/0911    
		mntfree(r); 
		if(alloc) 
1997/0327    
			cclose(nc); 
1990/0227    
		nexterror(); 
	} 
1991/0911    
 
	r->request.type = Tclone; 
	r->request.fid = c->fid; 
	r->request.newfid = nc->fid; 
	mountrpc(m, r); 
 
1993/0304    
	devclone(c, nc); 
1990/0303    
	nc->mqid = c->qid; 
1991/0904    
	incref(m); 
1991/0911    
 
1992/0312    
	USED(alloc); 
1991/0911    
	poperror(); 
	mntfree(r); 
1990/0227    
	return nc; 
} 
 
1998/0512    
static int 
1990/0227    
mntwalk(Chan *c, char *name) 
{ 
	Mnt *m; 
1991/0911    
	Mntrpc *r; 
1990/0227    
 
1991/0911    
	m = mntchk(c); 
1993/0501    
	r = mntralloc(c); 
1991/0911    
	if(waserror()) { 
		mntfree(r); 
		return 0; 
1990/0227    
	} 
1991/0911    
	r->request.type = Twalk; 
	r->request.fid = c->fid; 
	strncpy(r->request.name, name, NAMELEN); 
	mountrpc(m, r); 
 
	c->qid = r->reply.qid; 
1993/0808    
 
1990/0227    
	poperror(); 
1991/0911    
	mntfree(r); 
	return 1; 
1990/0227    
} 
 
1998/0512    
static void 
1990/0227    
mntstat(Chan *c, char *dp) 
{ 
	Mnt *m; 
1991/0911    
	Mntrpc *r; 
1990/0227    
 
1991/0911    
	m = mntchk(c); 
1993/0501    
	r = mntralloc(c); 
1991/0911    
	if(waserror()) { 
		mntfree(r); 
1990/0227    
		nexterror(); 
	} 
1991/0911    
	r->request.type = Tstat; 
	r->request.fid = c->fid; 
	mountrpc(m, r); 
 
	memmove(dp, r->reply.stat, DIRLEN); 
	mntdirfix((uchar*)dp, c); 
1990/0227    
	poperror(); 
1991/0911    
	mntfree(r); 
1990/0227    
} 
 
1997/0327    
static Chan* 
1990/0227    
mntopen(Chan *c, int omode) 
{ 
	Mnt *m; 
1991/0911    
	Mntrpc *r; 
1990/0227    
 
1991/0911    
	m = mntchk(c); 
1993/0501    
	r = mntralloc(c); 
1991/0911    
	if(waserror()) { 
		mntfree(r); 
1990/0227    
		nexterror(); 
	} 
1991/0911    
	r->request.type = Topen; 
	r->request.fid = c->fid; 
	r->request.mode = omode; 
	mountrpc(m, r); 
 
	c->qid = r->reply.qid; 
1990/0227    
	c->offset = 0; 
	c->mode = openmode(omode); 
	c->flag |= COPEN; 
1991/0911    
	poperror(); 
	mntfree(r); 
1993/1015    
 
1993/1016    
	if(c->flag & CCACHE) 
1993/1015    
		copen(c); 
 
1990/0227    
	return c; 
} 
 
1998/0512    
static void 
1990/0227    
mntcreate(Chan *c, char *name, int omode, ulong perm) 
{ 
	Mnt *m; 
1991/0911    
	Mntrpc *r; 
1990/0227    
 
1991/0911    
	m = mntchk(c); 
1993/0501    
	r = mntralloc(c); 
1991/0911    
	if(waserror()) { 
		mntfree(r); 
1990/0227    
		nexterror(); 
	} 
1991/0911    
	r->request.type = Tcreate; 
	r->request.fid = c->fid; 
	r->request.mode = omode; 
	r->request.perm = perm; 
	strncpy(r->request.name, name, NAMELEN); 
	mountrpc(m, r); 
 
	c->qid = r->reply.qid; 
1990/0227    
	c->flag |= COPEN; 
	c->mode = openmode(omode); 
1991/0911    
	poperror(); 
	mntfree(r); 
1993/1015    
 
1993/1016    
	if(c->flag & CCACHE) 
1993/1015    
		copen(c); 
1990/0227    
} 
 
1998/0512    
static void 
1990/0604    
mntclunk(Chan *c, int t) 
1990/0227    
{ 
	Mnt *m; 
1991/1004    
	Mntrpc *r; 
1998/0512    
 
1991/0911    
	m = mntchk(c); 
1993/0501    
	r = mntralloc(c); 
1991/0911    
	if(waserror()){ 
1993/0501    
		mntfree(r); 
		mclose(m, c); 
1991/1004    
		nexterror(); 
1991/0911    
	} 
1990/0227    
 
1991/0911    
	r->request.type = t; 
	r->request.fid = c->fid; 
	mountrpc(m, r); 
1993/0501    
	mntfree(r); 
	mclose(m, c); 
1991/1004    
	poperror(); 
} 
 
void 
1999/0714    
mclose(Mnt *m, Chan*) 
1991/1004    
{ 
1993/0321    
	Mntrpc *q, *r; 
1991/1004    
 
1993/0321    
	if(decref(m) != 0) 
		return; 
 
	for(q = m->queue; q; q = r) { 
		r = q->list; 
		mntfree(q); 
	} 
	m->id = 0; 
1997/0327    
	cclose(m->c); 
1993/0321    
	mntpntfree(m); 
} 
 
void 
1992/0320    
mntpntfree(Mnt *m) 
{ 
1992/0620    
	Mnt *f, **l; 
 
1992/0320    
	lock(&mntalloc); 
1992/0620    
	l = &mntalloc.list; 
	for(f = *l; f; f = f->list) { 
		if(f == m) { 
			*l = m->list; 
			break; 
		} 
		l = &f->list; 
	} 
 
1992/0320    
	m->list = mntalloc.mntfree; 
	mntalloc.mntfree = m; 
	unlock(&mntalloc); 
1990/0227    
} 
 
1997/0327    
static void 
1990/0604    
mntclose(Chan *c) 
{ 
	mntclunk(c, Tclunk); 
} 
 
1998/0512    
static void 
1991/0911    
mntremove(Chan *c) 
1990/0227    
{ 
1991/0911    
	mntclunk(c, Tremove); 
} 
 
1997/0327    
static void 
1991/0911    
mntwstat(Chan *c, char *dp) 
{ 
1990/0227    
	Mnt *m; 
1991/0911    
	Mntrpc *r; 
1990/0227    
 
1991/0911    
	m = mntchk(c); 
1993/0501    
	r = mntralloc(c); 
1991/0911    
	if(waserror()) { 
		mntfree(r); 
1990/0227    
		nexterror(); 
	} 
1991/0911    
	r->request.type = Twstat; 
	r->request.fid = c->fid; 
	memmove(r->request.stat, dp, DIRLEN); 
	mountrpc(m, r); 
1990/0227    
	poperror(); 
1991/0911    
	mntfree(r); 
1990/0227    
} 
 
1998/0512    
long 
1998/0327    
mntread9p(Chan *c, void *buf, long n, vlong off) 
1994/0405    
{ 
1998/0327    
	return mnt9prdwr(Tread, c, buf, n, off); 
1994/0405    
} 
 
1998/0512    
static long 
1998/0319    
mntread(Chan *c, void *buf, long n, vlong off) 
1990/0227    
{ 
1991/0911    
	uchar *p, *e; 
1995/0204    
	int nc, cache, isdir; 
1990/0227    
 
1995/0204    
	isdir = 0; 
	cache = c->flag & CCACHE; 
	if(c->qid.path & CHDIR) { 
		cache = 0; 
		isdir = 1; 
	} 
 
1995/0129    
	p = buf; 
1995/0204    
	if(cache) { 
1998/0327    
		nc = cread(c, buf, n, off); 
1993/1015    
		if(nc > 0) { 
1993/1102    
			n -= nc; 
			if(n == 0) 
				return nc; 
1995/0129    
			p += nc; 
1998/0327    
			off += nc; 
1993/1015    
		} 
1998/0327    
		n = mntrdwr(Tread, c, p, n, off); 
		cupdate(c, p, n, off); 
1995/0129    
		return n + nc; 
1993/1014    
	} 
 
1998/0327    
	n = mntrdwr(Tread, c, buf, n, off); 
1995/0204    
	if(isdir) { 
		for(e = &p[n]; p < e; p += DIRLEN) 
			mntdirfix(p, c); 
	} 
1993/1014    
 
1990/0227    
	return n; 
} 
 
1998/0512    
long 
1998/0327    
mntwrite9p(Chan *c, void *buf, long n, vlong off) 
1994/0402    
{ 
1998/0327    
	return mnt9prdwr(Twrite, c, buf, n, off); 
1994/0402    
} 
 
1998/0512    
static long 
1998/0319    
mntwrite(Chan *c, void *buf, long n, vlong off) 
1990/0227    
{ 
1998/0327    
	return mntrdwr(Twrite, c, buf, n, off); 
1995/0108    
} 
 
long 
1998/0327    
mnt9prdwr(int type, Chan *c, void *buf, long n, vlong off) 
1990/0227    
{ 
1993/1016    
	Mnt *m; 
1995/0129    
 	ulong nr; 
	Mntrpc *r; 
 
	if(n > MAXRPC-32) { 
		if(type == Twrite) 
			error("write9p too long"); 
		n = MAXRPC-32; 
	} 
 
	m = mntchk(c); 
	r = mntralloc(c); 
	if(waserror()) { 
		mntfree(r); 
		nexterror(); 
	} 
	r->request.type = type; 
	r->request.fid = c->fid; 
1998/0327    
	r->request.offset = off; 
1995/0129    
	r->request.data = buf; 
	r->request.count = n; 
	mountrpc(m, r); 
	nr = r->reply.count; 
	if(nr > r->request.count) 
		nr = r->request.count; 
 
	if(type == Tread) 
		memmove(buf, r->reply.data, nr); 
 
	poperror(); 
	mntfree(r); 
	return nr; 
} 
 
long 
1998/0327    
mntrdwr(int type, Chan *c, void *buf, long n, vlong off) 
1995/0129    
{ 
	Mnt *m; 
1993/1016    
 	Mntrpc *r; 
1991/0911    
	char *uba; 
1993/1015    
	int cache; 
1998/0908    
	ulong cnt, nr, nreq; 
1990/0227    
 
1993/1016    
	m = mntchk(c); 
1991/0911    
	uba = buf; 
1993/0907    
	cnt = 0; 
1993/1016    
	cache = c->flag & CCACHE; 
1995/0206    
	if(c->qid.path & CHDIR) 
1995/0204    
		cache = 0; 
1993/0907    
	for(;;) { 
1993/0501    
		r = mntralloc(c); 
1991/0911    
		if(waserror()) { 
			mntfree(r); 
			nexterror(); 
		} 
		r->request.type = type; 
		r->request.fid = c->fid; 
1998/0327    
		r->request.offset = off; 
1991/0911    
		r->request.data = uba; 
1999/0722    
		if(n > m->blocksize){ 
			if(c->qid.path & CHDIR) 
				r->request.count = (m->blocksize/DIRLEN)*DIRLEN; 
			else 
				r->request.count = m->blocksize; 
		} else 
			r->request.count = n; 
1991/0911    
		mountrpc(m, r); 
1998/0908    
		nreq = r->request.count; 
1991/0911    
		nr = r->reply.count; 
1998/0908    
		if(nr > nreq) 
			nr = nreq; 
1993/1015    
 
1991/0911    
		if(type == Tread) 
			memmove(uba, r->reply.data, nr); 
1993/1015    
		else if(cache) 
1998/0327    
			cwrite(c, (uchar*)uba, nr, off); 
1993/1015    
 
1991/0911    
		poperror(); 
		mntfree(r); 
1998/0327    
		off += nr; 
1991/0911    
		uba += nr; 
		cnt += nr; 
1993/0907    
		n -= nr; 
1998/0908    
		if(nr != nreq || n == 0 || up->nnote) 
1991/0911    
			break; 
1990/0227    
	} 
1991/0911    
	return cnt; 
1990/0227    
} 
 
void 
1991/0911    
mountrpc(Mnt *m, Mntrpc *r) 
1990/0604    
{ 
1993/0501    
	int t; 
 
	r->reply.tag = 0; 
2000/0102    
	r->reply.type = Tmax;	/* can't ever be a valid message type */ 
1992/0620    
 
1991/0911    
	mountio(m, r); 
1992/0620    
 
1993/0501    
	t = r->reply.type; 
	switch(t) { 
	case Rerror: 
		error(r->reply.ename); 
	case Rflush: 
1992/0111    
		error(Eintr); 
1993/0501    
	default: 
		if(t == r->request.type+1) 
			break; 
2000/0104    
		print("mnt: proc %s %lud: mismatch rep 0x%lux tag %d fid %d T%d R%d rp %d\n", 
1997/0405    
			up->text, up->pid, 
1999/1018    
			r, r->request.tag, r->request.fid, r->request.type, r->reply.type, 
2000/0104    
			r->reply.tag); 
1992/0113    
		error(Emountrpc); 
1990/1123    
	} 
} 
 
void 
1991/0911    
mountio(Mnt *m, Mntrpc *r) 
1990/1123    
{ 
1991/0911    
	int n; 
1990/1124    
 
2000/0105    
	while(waserror()) { 
		if(m->rip == up) 
			mntgate(m); 
		if(strcmp(up->error, Eintr) != 0){ 
			mntflushfree(m, r); 
			nexterror(); 
		} 
		r = mntflushalloc(r); 
	} 
 
1991/0911    
	lock(m); 
	r->m = m; 
	r->list = m->queue; 
	m->queue = r; 
	unlock(m); 
 
	/* Transmit a file system rpc */ 
	n = convS2M(&r->request, r->rpc); 
1997/0327    
	if(n < 0) 
		panic("bad message type in mountio"); 
2000/0104    
	if(devtab[m->c->type]->dc == L'M'){ 
		if(mnt9prdwr(Twrite, m->c, r->rpc, n, 0) != n) 
			error(Emountrpc); 
	}else{ 
		if(devtab[m->c->type]->write(m->c, r->rpc, n, 0) != n) 
			error(Emountrpc); 
1991/0925    
	} 
2000/0104    
	r->stime = fastticks(nil); 
	r->reqlen = n; 
1990/1124    
 
1991/0911    
	/* Gate readers onto the mount point one at a time */ 
	for(;;) { 
		lock(m); 
		if(m->rip == 0) 
			break; 
		unlock(m); 
		sleep(&r->r, rpcattn, r); 
2000/0104    
		if(r->done){ 
			poperror(); 
2000/0105    
			mntflushfree(m, r); 
1991/0911    
			return; 
2000/0104    
		} 
1990/1124    
	} 
1993/0501    
	m->rip = up; 
1991/0911    
	unlock(m); 
	while(r->done == 0) { 
		mntrpcread(m, r); 
		mountmux(m, r); 
	} 
	mntgate(m); 
2000/0104    
	poperror(); 
2000/0105    
	mntflushfree(m, r); 
1990/1124    
} 
 
void 
1991/0911    
mntrpcread(Mnt *m, Mntrpc *r) 
1990/1124    
{ 
1993/1103    
	int n; 
1992/0505    
 
1991/0911    
	for(;;) { 
		r->reply.type = 0; 
		r->reply.tag = 0; 
1997/0408    
		if(devtab[m->c->type]->dc == L'M') 
1995/0129    
			n = mnt9prdwr(Tread, m->c, r->rpc, MAXRPC, 0); 
1994/0405    
		else 
1997/0327    
			n = devtab[m->c->type]->read(m->c, r->rpc, MAXRPC, 0); 
1993/1103    
		if(n == 0) 
1991/0911    
			continue; 
1992/0503    
 
1998/0917    
		r->replen = n; 
1991/0911    
		if(convM2S(r->rpc, &r->reply, n) != 0) 
			return; 
	} 
1990/0604    
} 
1990/11211    
 
1991/0911    
void 
mntgate(Mnt *m) 
1990/0717    
{ 
1991/0911    
	Mntrpc *q; 
 
	lock(m); 
	m->rip = 0; 
1993/0501    
	for(q = m->queue; q; q = q->list) { 
1997/0220    
		if(q->done == 0) 
		if(wakeup(&q->r)) 
			break; 
1993/0501    
	} 
1991/0911    
	unlock(m); 
1990/0717    
} 
1990/11211    
 
1990/0604    
void 
1991/0911    
mountmux(Mnt *m, Mntrpc *r) 
1990/0227    
{ 
1991/0911    
	char *dp; 
1992/0825    
	Mntrpc **l, *q; 
1990/0227    
 
1991/0911    
	lock(m); 
	l = &m->queue; 
	for(q = *l; q; q = q->list) { 
2000/0105    
		/* look for a reply to a message */ 
2000/0104    
		if(q->request.tag == r->reply.tag) { 
1991/0911    
			*l = q->list; 
1999/0212    
			if(q != r) { 
				/* 
				 * Completed someone else. 
				 * Trade pointers to receive buffer. 
				 */ 
1992/0305    
				dp = q->rpc; 
				q->rpc = r->rpc; 
				r->rpc = dp; 
1993/0501    
				q->reply = r->reply; 
1998/0916    
			} 
2000/0102    
			q->done = 1; 
			unlock(m); 
2000/0101    
			if(mntstats != nil) 
				(*mntstats)(q->request.type, 
					m->c, q->stime, 
					q->reqlen + r->replen); 
			if(q != r) 
				wakeup(&q->r); 
1991/0911    
			return; 
		} 
		l = &q->list; 
1990/0227    
	} 
1991/0911    
	unlock(m); 
} 
1990/0227    
 
2000/0105    
/* 
 * Create a new flush request and chain the previous 
 * requests from it 
 */ 
Mntrpc* 
mntflushalloc(Mntrpc *r) 
1991/0911    
{ 
2000/0104    
	Mntrpc *fr; 
1990/0511    
 
2000/0104    
	fr = mntralloc(0); 
1990/0511    
 
2000/0104    
	fr->request.type = Tflush; 
	if(r->request.type == Tflush) 
		fr->request.oldtag = r->request.oldtag; 
	else 
		fr->request.oldtag = r->request.tag; 
2000/0105    
	fr->flushed = r; 
1991/0911    
 
2000/0105    
	return fr; 
} 
2000/0104    
 
2000/0105    
/* 
 *  Free a chain of flushes.  Remove each unanswered 
 *  flush and the original message from the unanswered 
 *  request queue.  Mark the original message as done 
 *  and if it hasn't been answered set the reply to to 
 *  Rflush. 
 */ 
void 
mntflushfree(Mnt *m, Mntrpc *r) 
{ 
	Mntrpc *fr; 
 
	while(r){ 
		fr = r->flushed; 
		if(!r->done){ 
			r->reply.type = Rflush; 
			mntqrm(m, r); 
		} 
		if(fr) 
			mntfree(r); 
		r = fr; 
1990/03081    
	} 
1991/0911    
} 
1991/0901    
 
1997/0327    
Mntrpc* 
1993/0501    
mntralloc(Chan *c) 
1991/0911    
{ 
	Mntrpc *new; 
1991/0901    
 
1992/0620    
	lock(&mntalloc); 
	new = mntalloc.rpcfree; 
1999/0212    
	if(new == nil){ 
		new = malloc(sizeof(Mntrpc)); 
		if(new == nil) { 
1991/0911    
			unlock(&mntalloc); 
1999/0212    
			exhausted("mount rpc header"); 
		} 
		/* 
		 * The header is split from the data buffer as 
		 * mountmux may swap the buffer with another header. 
		 */ 
		new->rpc = mallocz(MAXRPC, 0); 
		if(new->rpc == nil){ 
			free(new); 
			unlock(&mntalloc); 
1992/0620    
			exhausted("mount rpc buffer"); 
1991/0911    
		} 
1992/0620    
		new->request.tag = mntalloc.rpctag++; 
1991/0904    
	} 
1999/0212    
	else { 
		mntalloc.rpcfree = new->list; 
		mntalloc.nrpcfree--; 
	} 
	mntalloc.nrpcused++; 
1992/0620    
	unlock(&mntalloc); 
1993/0501    
	new->c = c; 
1992/0620    
	new->done = 0; 
2000/0105    
	new->flushed = nil; 
1992/0620    
	return new; 
1991/0911    
} 
 
void 
mntfree(Mntrpc *r) 
{ 
	lock(&mntalloc); 
1999/0212    
	if(mntalloc.nrpcfree >= 10){ 
		free(r->rpc); 
		free(r); 
	} 
	else{ 
		r->list = mntalloc.rpcfree; 
		mntalloc.rpcfree = r; 
		mntalloc.nrpcfree++; 
	} 
	mntalloc.nrpcused--; 
1991/0911    
	unlock(&mntalloc); 
} 
 
void 
mntqrm(Mnt *m, Mntrpc *r) 
{ 
	Mntrpc **l, *f; 
 
	lock(m); 
	r->done = 1; 
 
	l = &m->queue; 
	for(f = *l; f; f = f->list) { 
		if(f == r) { 
			*l = r->list; 
			break; 
1990/0703    
		} 
1991/0911    
		l = &f->list; 
1990/0227    
	} 
1991/0911    
	unlock(m); 
} 
1990/0227    
 
1997/0327    
Mnt* 
1991/0911    
mntchk(Chan *c) 
{ 
	Mnt *m; 
 
1992/0620    
	m = c->mntptr; 
1993/0501    
 
	/* 
	 * Was it closed and reused 
	 */ 
	if(m->id == 0 || m->id >= c->dev) 
1990/11211    
		error(Eshutdown); 
1993/0501    
 
1991/0911    
	return m; 
1990/0717    
} 
 
1990/1220    
void 
1991/0911    
mntdirfix(uchar *dirbuf, Chan *c) 
{ 
1997/0408    
	int r; 
 
	r = devtab[c->type]->dc; 
	dirbuf[DIRLEN-4] = r>>0; 
	dirbuf[DIRLEN-3] = r>>8; 
1991/0911    
	dirbuf[DIRLEN-2] = c->dev; 
	dirbuf[DIRLEN-1] = c->dev>>8; 
} 
 
int 
1999/0320    
rpcattn(void *v) 
1991/0911    
{ 
1999/0320    
	Mntrpc *r; 
 
	r = v; 
1991/0911    
	return r->done || r->m->rip == 0; 
} 
1997/0327    
 
Dev mntdevtab = { 
1997/0409    
	'M', 
1997/0408    
	"mnt", 
 
1997/0327    
	mntreset, 
	devinit, 
	mntattach, 
	mntclone, 
	mntwalk, 
	mntstat, 
	mntopen, 
	mntcreate, 
	mntclose, 
	mntread, 
	devbread, 
	mntwrite, 
	devbwrite, 
	mntremove, 
	mntwstat, 
}; 


source code copyright © 1990-2005 Lucent Technologies; see license
Plan 9 distribution
comments to russ cox (rsc@swtch.com)