#include "u.h"#include "../port/lib.h"#include "mem.h"#include "dat.h"#include "fns.h"#include "../port/error.h" /* * References are managed as follows: * The channel to the server - a network connection or pipe - has one * reference for every Chan open on the server. The server channel has * c->mux set to the Mnt used for muxing control to that server. Mnts * have no reference count; they go away when c goes away. * Each channel derived from the mount point has mchan set to c, * and increfs/decrefs mchan to manage references on the server * connection. */ #define MAXRPC (IOHDRSZ+8192) struct Mntrpc{ Chan* c; /* Channel for whom we are working */ Mntrpc* list; /* Free/pending list */ Fcall request; /* Outgoing file system protocol message */ Fcall reply; /* Incoming reply */ Mnt* m; /* Mount device during rpc */ Rendez r; /* Place to hang out */ uchar* rpc; /* I/O Data buffer */ uint rpclen; /* len of buffer */ Block *b; /* reply blocks */ char done; /* Rpc completed */ uvlong stime; /* start time for mnt statistics */ ulong reqlen; /* request length for mnt statistics */ ulong replen; /* reply length for mnt statistics */ Mntrpc* flushed; /* message this one flushes */}; enum{ TAGSHIFT = 5, /* ulong has to be 32 bits */ TAGMASK = (1<<TAGSHIFT)-1, NMASK = (64*1024)>>TAGSHIFT,}; struct Mntalloc{ Lock; Mnt* list; /* Mount devices in use */ Mnt* mntfree; /* Free list */ Mntrpc* rpcfree; int nrpcfree; int nrpcused; uint id; ulong tagmask[NMASK];}mntalloc; Mnt* mntchk(Chan*);void mntdirfix(uchar*, Chan*);Mntrpc* mntflushalloc(Mntrpc*, ulong);void mntflushfree(Mnt*, Mntrpc*);void mntfree(Mntrpc*);void mntgate(Mnt*);void mntpntfree(Mnt*);void mntqrm(Mnt*, Mntrpc*);Mntrpc* mntralloc(Chan*, ulong);long mntrdwr(int, Chan*, void*, long, vlong);int mntrpcread(Mnt*, Mntrpc*);void mountio(Mnt*, Mntrpc*);void mountmux(Mnt*, Mntrpc*);void mountrpc(Mnt*, Mntrpc*);int rpcattn(void*);Chan* mntchan(void); char Esbadstat[] = "invalid directory entry received from server";char Enoversion[] = "version not established for mount channel"; void (*mntstats)(int, Chan*, uvlong, ulong); static voidmntreset(void){ mntalloc.id = 1; mntalloc.tagmask[0] = 1; /* don't allow 0 as a tag */ mntalloc.tagmask[NMASK-1] = 0x80000000UL; /* don't allow NOTAG */ fmtinstall('F', fcallfmt); fmtinstall('D', dirfmt);/* We can't install %M since eipfmt does and is used in the kernel [sape] */ if(mfcinit != nil) mfcinit();} /* * Version is not multiplexed: message sent only once per connection. */usizemntversion(Chan *c, u32int msize, char *version, usize returnlen){ Fcall f; uchar *msg; Mnt *mnt; char *v; long l, n; usize k; vlong oo; char buf[128]; qlock(&c->umqlock); /* make sure no one else does this until we've established ourselves */ if(waserror()){ qunlock(&c->umqlock); nexterror(); } /* defaults */ if(msize == 0) msize = MAXRPC; if(msize > c->iounit && c->iounit != 0) msize = c->iounit; v = version; if(v == nil || v[0] == '\0') v = VERSION9P; /* validity */ if(msize < 0) error("bad iounit in version call"); if(strncmp(v, VERSION9P, strlen(VERSION9P)) != 0) error("bad 9P version specification"); mnt = c->mux; if(mnt != nil){ qunlock(&c->umqlock); poperror(); strecpy(buf, buf+sizeof buf, mnt->version); k = strlen(buf); if(strncmp(buf, v, k) != 0){ snprint(buf, sizeof buf, "incompatible 9P versions %s %s", mnt->version, v); error(buf); } if(returnlen != 0){ if(returnlen < k) error(Eshort); memmove(version, buf, k); } return k; } f.type = Tversion; f.tag = NOTAG; f.msize = msize; f.version = v; msg = malloc(8192+IOHDRSZ); if(msg == nil) exhausted("version memory"); if(waserror()){ free(msg); nexterror(); } k = convS2M(&f, msg, 8192+IOHDRSZ); if(k == 0) error("bad fversion conversion on send"); lock(c); oo = c->offset; c->offset += k; unlock(c); l = c->dev->write(c, msg, k, oo); if(l < k){ lock(c); c->offset -= k - l; unlock(c); error("short write in fversion"); } /* message sent; receive and decode reply */ n = c->dev->read(c, msg, 8192+IOHDRSZ, c->offset); if(n <= 0) error("EOF receiving fversion reply"); lock(c); c->offset += n; unlock(c); l = convM2S(msg, n, &f); if(l != n) error("bad fversion conversion on reply"); if(f.type != Rversion){ if(f.type == Rerror) error(f.ename); error("unexpected reply type in fversion"); } if(f.msize > msize) error("server tries to increase msize in fversion"); if(f.msize<256 || f.msize>1024*1024) error("nonsense value of msize in fversion"); k = strlen(f.version); if(strncmp(f.version, v, k) != 0) error("bad 9P version returned from server"); /* now build Mnt associated with this connection */ lock(&mntalloc); mnt = mntalloc.mntfree; if(mnt != nil) mntalloc.mntfree = mnt->list; else { mnt = malloc(sizeof(Mnt)); if(mnt == nil) { unlock(&mntalloc); exhausted("mount devices"); } } mnt->list = mntalloc.list; mntalloc.list = mnt; mnt->version = nil; kstrdup(&mnt->version, f.version); mnt->id = mntalloc.id++; mnt->q = qopen(10*MAXRPC, 0, nil, nil); mnt->msize = f.msize; unlock(&mntalloc); if(returnlen != 0){ if(returnlen < k) error(Eshort); memmove(version, f.version, k); } poperror(); /* msg */ free(msg); lock(mnt); mnt->queue = 0; mnt->rip = 0; c->flag |= CMSG; c->mux = mnt; mnt->c = c; unlock(mnt); poperror(); /* c */ qunlock(&c->umqlock); return k;} Chan*mntauth(Chan *c, char *spec){ Mnt *mnt; Mntrpc *r; mnt = c->mux; if(mnt == nil){ mntversion(c, MAXRPC, VERSION9P, 0); mnt = c->mux; if(mnt == nil) error(Enoversion); } c = mntchan(); if(waserror()) { /* Close must not be called since it will * call mnt recursively */ chanfree(c); nexterror(); } r = mntralloc(0, mnt->msize); if(waserror()) { mntfree(r); nexterror(); } r->request.type = Tauth; r->request.afid = c->fid; r->request.uname = up->user; r->request.aname = spec; mountrpc(mnt, r); c->qid = r->reply.aqid; c->mchan = mnt->c; incref(mnt->c); c->mqid = c->qid; c->mode = ORDWR; poperror(); /* r */ mntfree(r); poperror(); /* c */ return c; } static Chan*mntattach(char *muxattach){ Mnt *mnt; Chan *c; Mntrpc *r; struct bogus{ Chan *chan; Chan *authchan; char *spec; int flags; }bogus; bogus = *((struct bogus *)muxattach); c = bogus.chan; mnt = c->mux; if(mnt == nil){ mntversion(c, 0, nil, 0); mnt = c->mux; if(mnt == nil) error(Enoversion); } c = mntchan(); if(waserror()) { /* Close must not be called since it will * call mnt recursively */ chanfree(c); nexterror(); } r = mntralloc(0, mnt->msize); if(waserror()) { mntfree(r); nexterror(); } r->request.type = Tattach; r->request.fid = c->fid; if(bogus.authchan == nil) r->request.afid = NOFID; else r->request.afid = bogus.authchan->fid; r->request.uname = up->user; r->request.aname = bogus.spec; mountrpc(mnt, r); c->qid = r->reply.qid; c->mchan = mnt->c; incref(mnt->c); c->mqid = c->qid; poperror(); /* r */ mntfree(r); poperror(); /* c */ if((bogus.flags & MCACHE) && mfcinit != nil) c->flag |= CCACHE; return c;} Chan*mntchan(void){ Chan *c; c = devattach('M', 0); lock(&mntalloc); c->devno = mntalloc.id++; unlock(&mntalloc); if(c->mchan) panic("mntchan non-zero %#p", c->mchan); return c;} static Walkqid*mntwalk(Chan *c, Chan *nc, char **name, int nname){ int i, alloc; Mnt *mnt; Mntrpc *r; Walkqid *wq; if(nc != nil) print("mntwalk: nc != nil\n"); if(nname > MAXWELEM) error("devmnt: too many name elements"); alloc = 0; wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid)); if(waserror()){ if(alloc && wq->clone!=nil) cclose(wq->clone); free(wq); return nil; } alloc = 0; mnt = mntchk(c); r = mntralloc(c, mnt->msize); if(nc == nil){ nc = devclone(c); /* * Until the other side accepts this fid, * we can't mntclose it. * nc->dev remains nil for now. */ alloc = 1; } wq->clone = nc; if(waserror()) { mntfree(r); nexterror(); } r->request.type = Twalk; r->request.fid = c->fid; r->request.newfid = nc->fid; r->request.nwname = nname; memmove(r->request.wname, name, nname*sizeof(char*)); mountrpc(mnt, r); if(r->reply.nwqid > nname) error("too many QIDs returned by walk"); if(r->reply.nwqid < nname){ if(alloc) cclose(nc); wq->clone = nil; if(r->reply.nwqid == 0){ free(wq); wq = nil; goto Return; } } /* move new fid onto mnt device and update its qid */ if(wq->clone != nil){ if(wq->clone != c){ wq->clone->dev = c->dev; //if(wq->clone->dev != nil) //XDYNX // devtabincr(wq->clone->dev); wq->clone->mchan = c->mchan; incref(c->mchan); } if(r->reply.nwqid > 0) wq->clone->qid = r->reply.wqid[r->reply.nwqid-1]; } wq->nqid = r->reply.nwqid; for(i=0; i<wq->nqid; i++) wq->qid[i] = r->reply.wqid[i]; Return: poperror(); mntfree(r); poperror(); return wq;} static longmntstat(Chan *c, uchar *dp, long n){ Mnt *mnt; Mntrpc *r; usize nstat; if(n < BIT16SZ) error(Eshortstat); mnt = mntchk(c); r = mntralloc(c, mnt->msize); if(waserror()) { mntfree(r); nexterror(); } r->request.type = Tstat; r->request.fid = c->fid; mountrpc(mnt, r); if(r->reply.nstat > n){ nstat = BIT16SZ; PBIT16(dp, r->reply.nstat-2); }else{ nstat = r->reply.nstat; memmove(dp, r->reply.stat, nstat); validstat(dp, nstat); mntdirfix(dp, c); } poperror(); mntfree(r); return nstat;} static Chan*Lines 1–500 of 1204. Load more
#include "u.h" #include "../port/lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "../port/error.h" /* * References are managed as follows: * The channel to the server - a network connection or pipe - has one * reference for every Chan open on the server. The server channel has * c->mux set to the Mnt used for muxing control to that server. Mnts * have no reference count; they go away when c goes away. * Each channel derived from the mount point has mchan set to c, * and increfs/decrefs mchan to manage references on the server * connection. */ #define MAXRPC (IOHDRSZ+8192) struct Mntrpc { Chan* c; /* Channel for whom we are working */ Mntrpc* list; /* Free/pending list */ Fcall request; /* Outgoing file system protocol message */ Fcall reply; /* Incoming reply */ Mnt* m; /* Mount device during rpc */ Rendez r; /* Place to hang out */ uchar* rpc; /* I/O Data buffer */ uint rpclen; /* len of buffer */ Block *b; /* reply blocks */ char done; /* Rpc completed */ uvlong stime; /* start time for mnt statistics */ ulong reqlen; /* request length for mnt statistics */ ulong replen; /* reply length for mnt statistics */ Mntrpc* flushed; /* message this one flushes */ }; enum { TAGSHIFT = 5, /* ulong has to be 32 bits */ TAGMASK = (1<<TAGSHIFT)-1, NMASK = (64*1024)>>TAGSHIFT, }; struct Mntalloc { Lock; Mnt* list; /* Mount devices in use */ Mnt* mntfree; /* Free list */ Mntrpc* rpcfree; int nrpcfree; int nrpcused; uint id; ulong tagmask[NMASK]; }mntalloc; Mnt* mntchk(Chan*); void mntdirfix(uchar*, Chan*); Mntrpc* mntflushalloc(Mntrpc*, ulong); void mntflushfree(Mnt*, Mntrpc*); void mntfree(Mntrpc*); void mntgate(Mnt*); void mntpntfree(Mnt*); void mntqrm(Mnt*, Mntrpc*); Mntrpc* mntralloc(Chan*, ulong); long mntrdwr(int, Chan*, void*, long, vlong); int mntrpcread(Mnt*, Mntrpc*); void mountio(Mnt*, Mntrpc*); void mountmux(Mnt*, Mntrpc*); void mountrpc(Mnt*, Mntrpc*); int rpcattn(void*); Chan* mntchan(void); char Esbadstat[] = "invalid directory entry received from server"; char Enoversion[] = "version not established for mount channel"; void (*mntstats)(int, Chan*, uvlong, ulong); static void mntreset(void) { mntalloc.id = 1; mntalloc.tagmask[0] = 1; /* don't allow 0 as a tag */ mntalloc.tagmask[NMASK-1] = 0x80000000UL; /* don't allow NOTAG */ fmtinstall('F', fcallfmt); fmtinstall('D', dirfmt); /* We can't install %M since eipfmt does and is used in the kernel [sape] */ if(mfcinit != nil) mfcinit(); } /* * Version is not multiplexed: message sent only once per connection. */ usize mntversion(Chan *c, u32int msize, char *version, usize returnlen) { Fcall f; uchar *msg; Mnt *mnt; char *v; long l, n; usize k; vlong oo; char buf[128]; qlock(&c->umqlock); /* make sure no one else does this until we've established ourselves */ if(waserror()){ qunlock(&c->umqlock); nexterror(); } /* defaults */ if(msize == 0) msize = MAXRPC; if(msize > c->iounit && c->iounit != 0) msize = c->iounit; v = version; if(v == nil || v[0] == '\0') v = VERSION9P; /* validity */ if(msize < 0) error("bad iounit in version call"); if(strncmp(v, VERSION9P, strlen(VERSION9P)) != 0) error("bad 9P version specification"); mnt = c->mux; if(mnt != nil){ qunlock(&c->umqlock); poperror(); strecpy(buf, buf+sizeof buf, mnt->version); k = strlen(buf); if(strncmp(buf, v, k) != 0){ snprint(buf, sizeof buf, "incompatible 9P versions %s %s", mnt->version, v); error(buf); } if(returnlen != 0){ if(returnlen < k) error(Eshort); memmove(version, buf, k); } return k; } f.type = Tversion; f.tag = NOTAG; f.msize = msize; f.version = v; msg = malloc(8192+IOHDRSZ); if(msg == nil) exhausted("version memory"); if(waserror()){ free(msg); nexterror(); } k = convS2M(&f, msg, 8192+IOHDRSZ); if(k == 0) error("bad fversion conversion on send"); lock(c); oo = c->offset; c->offset += k; unlock(c); l = c->dev->write(c, msg, k, oo); if(l < k){ lock(c); c->offset -= k - l; unlock(c); error("short write in fversion"); } /* message sent; receive and decode reply */ n = c->dev->read(c, msg, 8192+IOHDRSZ, c->offset); if(n <= 0) error("EOF receiving fversion reply"); lock(c); c->offset += n; unlock(c); l = convM2S(msg, n, &f); if(l != n) error("bad fversion conversion on reply"); if(f.type != Rversion){ if(f.type == Rerror) error(f.ename); error("unexpected reply type in fversion"); } if(f.msize > msize) error("server tries to increase msize in fversion"); if(f.msize<256 || f.msize>1024*1024) error("nonsense value of msize in fversion"); k = strlen(f.version); if(strncmp(f.version, v, k) != 0) error("bad 9P version returned from server"); /* now build Mnt associated with this connection */ lock(&mntalloc); mnt = mntalloc.mntfree; if(mnt != nil) mntalloc.mntfree = mnt->list; else { mnt = malloc(sizeof(Mnt)); if(mnt == nil) { unlock(&mntalloc); exhausted("mount devices"); } } mnt->list = mntalloc.list; mntalloc.list = mnt; mnt->version = nil; kstrdup(&mnt->version, f.version); mnt->id = mntalloc.id++; mnt->q = qopen(10*MAXRPC, 0, nil, nil); mnt->msize = f.msize; unlock(&mntalloc); if(returnlen != 0){ if(returnlen < k) error(Eshort); memmove(version, f.version, k); } poperror(); /* msg */ free(msg); lock(mnt); mnt->queue = 0; mnt->rip = 0; c->flag |= CMSG; c->mux = mnt; mnt->c = c; unlock(mnt); poperror(); /* c */ qunlock(&c->umqlock); return k; } Chan* mntauth(Chan *c, char *spec) { Mnt *mnt; Mntrpc *r; mnt = c->mux; if(mnt == nil){ mntversion(c, MAXRPC, VERSION9P, 0); mnt = c->mux; if(mnt == nil) error(Enoversion); } c = mntchan(); if(waserror()) { /* Close must not be called since it will * call mnt recursively */ chanfree(c); nexterror(); } r = mntralloc(0, mnt->msize); if(waserror()) { mntfree(r); nexterror(); } r->request.type = Tauth; r->request.afid = c->fid; r->request.uname = up->user; r->request.aname = spec; mountrpc(mnt, r); c->qid = r->reply.aqid; c->mchan = mnt->c; incref(mnt->c); c->mqid = c->qid; c->mode = ORDWR; poperror(); /* r */ mntfree(r); poperror(); /* c */ return c; } static Chan* mntattach(char *muxattach) { Mnt *mnt; Chan *c; Mntrpc *r; struct bogus{ Chan *chan; Chan *authchan; char *spec; int flags; }bogus; bogus = *((struct bogus *)muxattach); c = bogus.chan; mnt = c->mux; if(mnt == nil){ mntversion(c, 0, nil, 0); mnt = c->mux; if(mnt == nil) error(Enoversion); } c = mntchan(); if(waserror()) { /* Close must not be called since it will * call mnt recursively */ chanfree(c); nexterror(); } r = mntralloc(0, mnt->msize); if(waserror()) { mntfree(r); nexterror(); } r->request.type = Tattach; r->request.fid = c->fid; if(bogus.authchan == nil) r->request.afid = NOFID; else r->request.afid = bogus.authchan->fid; r->request.uname = up->user; r->request.aname = bogus.spec; mountrpc(mnt, r); c->qid = r->reply.qid; c->mchan = mnt->c; incref(mnt->c); c->mqid = c->qid; poperror(); /* r */ mntfree(r); poperror(); /* c */ if((bogus.flags & MCACHE) && mfcinit != nil) c->flag |= CCACHE; return c; } Chan* mntchan(void) { Chan *c; c = devattach('M', 0); lock(&mntalloc); c->devno = mntalloc.id++; unlock(&mntalloc); if(c->mchan) panic("mntchan non-zero %#p", c->mchan); return c; } static Walkqid* mntwalk(Chan *c, Chan *nc, char **name, int nname) { int i, alloc; Mnt *mnt; Mntrpc *r; Walkqid *wq; if(nc != nil) print("mntwalk: nc != nil\n"); if(nname > MAXWELEM) error("devmnt: too many name elements"); alloc = 0; wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid)); if(waserror()){ if(alloc && wq->clone!=nil) cclose(wq->clone); free(wq); return nil; } alloc = 0; mnt = mntchk(c); r = mntralloc(c, mnt->msize); if(nc == nil){ nc = devclone(c); /* * Until the other side accepts this fid, * we can't mntclose it. * nc->dev remains nil for now. */ alloc = 1; } wq->clone = nc; if(waserror()) { mntfree(r); nexterror(); } r->request.type = Twalk; r->request.fid = c->fid; r->request.newfid = nc->fid; r->request.nwname = nname; memmove(r->request.wname, name, nname*sizeof(char*)); mountrpc(mnt, r); if(r->reply.nwqid > nname) error("too many QIDs returned by walk"); if(r->reply.nwqid < nname){ if(alloc) cclose(nc); wq->clone = nil; if(r->reply.nwqid == 0){ free(wq); wq = nil; goto Return; } } /* move new fid onto mnt device and update its qid */ if(wq->clone != nil){ if(wq->clone != c){ wq->clone->dev = c->dev; //if(wq->clone->dev != nil) //XDYNX // devtabincr(wq->clone->dev); wq->clone->mchan = c->mchan; incref(c->mchan); } if(r->reply.nwqid > 0) wq->clone->qid = r->reply.wqid[r->reply.nwqid-1]; } wq->nqid = r->reply.nwqid; for(i=0; i<wq->nqid; i++) wq->qid[i] = r->reply.wqid[i]; Return: poperror(); mntfree(r); poperror(); return wq; } static long mntstat(Chan *c, uchar *dp, long n) { Mnt *mnt; Mntrpc *r; usize nstat; if(n < BIT16SZ) error(Eshortstat); mnt = mntchk(c); r = mntralloc(c, mnt->msize); if(waserror()) { mntfree(r); nexterror(); } r->request.type = Tstat; r->request.fid = c->fid; mountrpc(mnt, r); if(r->reply.nstat > n){ nstat = BIT16SZ; PBIT16(dp, r->reply.nstat-2); }else{ nstat = r->reply.nstat; memmove(dp, r->reply.stat, nstat); validstat(dp, nstat); mntdirfix(dp, c); } poperror(); mntfree(r); return nstat; } static Chan*