2 #include "../port/lib.h"
6 #include "../port/error.h"
9 * References are managed as follows:
10 * The channel to the server - a network connection or pipe - has one
11 * reference for every Chan open on the server. The server channel has
12 * c->mux set to the Mnt used for muxing control to that server. Mnts
13 * have no reference count; they go away when c goes away.
14 * Each channel derived from the mount point has mchan set to c,
15 * and increfs/decrefs mchan to manage references on the server
19 #define MAXRPC (IOHDRSZ+8192)
23 Chan* c; /* Channel for whom we are working */
24 Mntrpc* list; /* Free/pending list */
25 Fcall request; /* Outgoing file system protocol message */
26 Fcall reply; /* Incoming reply */
27 Mnt* m; /* Mount device during rpc */
28 Rendez r; /* Place to hang out */
29 uchar* rpc; /* I/O Data buffer */
30 uint rpclen; /* len of buffer */
31 Block *b; /* reply blocks */
32 char done; /* Rpc completed */
33 uvlong stime; /* start time for mnt statistics */
34 ulong reqlen; /* request length for mnt statistics */
35 ulong replen; /* reply length for mnt statistics */
36 Mntrpc* flushed; /* message this one flushes */
41 TAGSHIFT = 5, /* ulong has to be 32 bits */
42 TAGMASK = (1<<TAGSHIFT)-1,
43 NMASK = (64*1024)>>TAGSHIFT,
49 Mnt* list; /* Mount devices in use */
50 Mnt* mntfree; /* Free list */
59 void mntdirfix(uchar*, Chan*);
60 Mntrpc* mntflushalloc(Mntrpc*, ulong);
61 void mntflushfree(Mnt*, Mntrpc*);
62 void mntfree(Mntrpc*);
64 void mntpntfree(Mnt*);
65 void mntqrm(Mnt*, Mntrpc*);
66 Mntrpc* mntralloc(Chan*, ulong);
67 long mntrdwr(int, Chan*, void*, long, vlong);
68 int mntrpcread(Mnt*, Mntrpc*);
69 void mountio(Mnt*, Mntrpc*);
70 void mountmux(Mnt*, Mntrpc*);
71 void mountrpc(Mnt*, Mntrpc*);
75 char Esbadstat[] = "invalid directory entry received from server";
76 char Enoversion[] = "version not established for mount channel";
79 void (*mntstats)(int, Chan*, uvlong, ulong);
85 mntalloc.tagmask[0] = 1; /* don't allow 0 as a tag */
86 mntalloc.tagmask[NMASK-1] = 0x80000000UL; /* don't allow NOTAG */
87 fmtinstall('F', fcallfmt);
88 fmtinstall('D', dirfmt);
89 /* We can't install %M since eipfmt does and is used in the kernel [sape] */
95 * Version is not multiplexed: message sent only once per connection.
98 mntversion(Chan *c, char *version, int msize, int returnlen)
108 eqlock(&c->umqlock); /* make sure no one else does this until we've established ourselves */
110 qunlock(&c->umqlock);
117 if(msize > c->iounit && c->iounit != 0)
120 if(v == nil || v[0] == '\0')
125 error("bad iounit in version call");
126 if(strncmp(v, VERSION9P, strlen(VERSION9P)) != 0)
127 error("bad 9P version specification");
132 qunlock(&c->umqlock);
135 strecpy(buf, buf+sizeof buf, m->version);
137 if(strncmp(buf, v, k) != 0){
138 snprint(buf, sizeof buf, "incompatible 9P versions %s %s", m->version, v);
144 memmove(version, buf, k);
153 msg = malloc(8192+IOHDRSZ);
155 exhausted("version memory");
160 k = convS2M(&f, msg, 8192+IOHDRSZ);
162 error("bad fversion conversion on send");
169 l = devtab[c->type]->write(c, msg, k, oo);
175 error("short write in fversion");
178 /* message sent; receive and decode reply */
179 k = devtab[c->type]->read(c, msg, 8192+IOHDRSZ, c->offset);
181 error("EOF receiving fversion reply");
187 l = convM2S(msg, k, &f);
189 error("bad fversion conversion on reply");
190 if(f.type != Rversion){
193 error("unexpected reply type in fversion");
196 error("server tries to increase msize in fversion");
197 if(f.msize<256 || f.msize>1024*1024)
198 error("nonsense value of msize in fversion");
199 k = strlen(f.version);
200 if(strncmp(f.version, v, k) != 0)
201 error("bad 9P version returned from server");
203 /* now build Mnt associated with this connection */
205 m = mntalloc.mntfree;
207 mntalloc.mntfree = m->list;
209 m = malloc(sizeof(Mnt));
212 exhausted("mount devices");
215 m->list = mntalloc.list;
218 kstrdup(&m->version, f.version);
219 m->id = mntalloc.id++;
220 m->q = qopen(10*MAXRPC, 0, nil, nil);
227 memmove(version, f.version, k);
230 poperror(); /* msg */
243 qunlock(&c->umqlock);
249 mntauth(Chan *c, char *spec)
257 mntversion(c, VERSION9P, MAXRPC, 0);
265 /* Close must not be called since it will
266 * call mnt recursively
272 r = mntralloc(0, m->msize);
279 r->request.type = Tauth;
280 r->request.afid = c->fid;
281 r->request.uname = up->user;
282 r->request.aname = spec;
285 c->qid = r->reply.aqid;
301 mntattach(char *muxattach)
313 bogus = *((struct bogus *)muxattach);
319 mntversion(c, nil, 0, 0);
327 /* Close must not be called since it will
328 * call mnt recursively
334 r = mntralloc(0, m->msize);
341 r->request.type = Tattach;
342 r->request.fid = c->fid;
343 if(bogus.authchan == nil)
344 r->request.afid = NOFID;
346 r->request.afid = bogus.authchan->fid;
347 r->request.uname = up->user;
348 r->request.aname = bogus.spec;
351 c->qid = r->reply.qid;
361 if(bogus.flags&MCACHE)
371 c = devattach('M', 0);
373 c->dev = mntalloc.id++;
377 panic("mntchan non-zero %p", c->mchan);
382 mntwalk(Chan *c, Chan *nc, char **name, int nname)
390 print("mntwalk: nc != nil\n");
392 error("devmnt: too many name elements");
394 wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid));
396 if(alloc && wq->clone!=nil)
404 r = mntralloc(c, m->msize);
408 * Until the other side accepts this fid, we can't mntclose it.
409 * Therefore set type to 0 for now; rootclose is known to be safe.
412 nc->flag |= (c->flag & CCACHE);
421 r->request.type = Twalk;
422 r->request.fid = c->fid;
423 r->request.newfid = nc->fid;
424 r->request.nwname = nname;
425 memmove(r->request.wname, name, nname*sizeof(char*));
429 if(r->reply.nwqid > nname)
430 error("too many QIDs returned by walk");
431 if(r->reply.nwqid < nname){
435 if(r->reply.nwqid == 0){
442 /* move new fid onto mnt device and update its qid */
443 if(wq->clone != nil){
445 wq->clone->type = c->type;
446 wq->clone->mchan = c->mchan;
449 if(r->reply.nwqid > 0)
450 wq->clone->qid = r->reply.wqid[r->reply.nwqid-1];
452 wq->nqid = r->reply.nwqid;
453 for(i=0; i<wq->nqid; i++)
454 wq->qid[i] = r->reply.wqid[i];
464 mntstat(Chan *c, uchar *dp, int n)
472 r = mntralloc(c, m->msize);
477 r->request.type = Tstat;
478 r->request.fid = c->fid;
481 if(r->reply.nstat > n){
483 PBIT16((uchar*)dp, r->reply.nstat-2);
486 memmove(dp, r->reply.stat, n);
496 mntopencreate(int type, Chan *c, char *name, int omode, ulong perm)
502 r = mntralloc(c, m->msize);
507 r->request.type = type;
508 r->request.fid = c->fid;
509 r->request.mode = omode;
511 r->request.perm = perm;
512 r->request.name = name;
516 c->qid = r->reply.qid;
518 c->mode = openmode(omode);
519 c->iounit = r->reply.iounit;
520 if(c->iounit == 0 || c->iounit > m->msize-IOHDRSZ)
521 c->iounit = m->msize-IOHDRSZ;
533 mntopen(Chan *c, int omode)
535 return mntopencreate(Topen, c, nil, omode, 0);
539 mntcreate(Chan *c, char *name, int omode, ulong perm)
541 return mntopencreate(Tcreate, c, name, omode, perm);
545 mntclunk(Chan *c, int t)
551 r = mntralloc(c, m->msize);
558 r->request.fid = c->fid;
569 for(q = m->queue; q; q = r) {
587 for(f = *l; f; f = f->list) {
594 m->list = mntalloc.mntfree;
595 mntalloc.mntfree = m;
611 mntclunk(c, Tremove);
615 mntwstat(Chan *c, uchar *dp, int n)
621 r = mntralloc(c, m->msize);
626 r->request.type = Twstat;
627 r->request.fid = c->fid;
628 r->request.nstat = n;
629 r->request.stat = dp;
637 mntread(Chan *c, void *buf, long n, vlong off)
640 int nc, cache, isdir, dirlen;
643 cache = c->flag & CCACHE;
644 if(c->qid.type & QTDIR) {
651 nc = cread(c, buf, n, off);
659 n = mntrdwr(Tread, c, p, n, off);
660 cupdate(c, p, n, off);
664 n = mntrdwr(Tread, c, buf, n, off);
666 for(e = &p[n]; p+BIT16SZ < e; p += dirlen){
667 dirlen = BIT16SZ+GBIT16(p);
670 validstat(p, dirlen);
680 mntwrite(Chan *c, void *buf, long n, vlong off)
682 return mntrdwr(Twrite, c, buf, n, off);
686 mntrdwr(int type, Chan *c, void *buf, long n, vlong off)
697 cache = c->flag & CCACHE;
698 if(c->qid.type & QTDIR)
701 r = mntralloc(c, m->msize);
706 r->request.type = type;
707 r->request.fid = c->fid;
708 r->request.offset = off;
709 r->request.data = uba;
711 if(nr > m->msize-IOHDRSZ)
712 nr = m->msize-IOHDRSZ;
713 r->request.count = nr;
715 nreq = r->request.count;
721 r->b = bl2mem((uchar*)uba, r->b, nr);
723 cwrite(c, (uchar*)uba, nr, off);
731 if(nr != nreq || n == 0 || up->nnote)
738 mountrpc(Mnt *m, Mntrpc *r)
744 r->reply.type = Tmax; /* can't ever be a valid message type */
751 error(r->reply.ename);
755 if(t == r->request.type+1)
758 if(m->c->path != nil)
761 if(r->c != nil && r->c->path != nil)
763 print("mnt: proc %s %lud: mismatch from %s %s rep %#p tag %d fid %d T%d R%d rp %d\n",
764 up->text, up->pid, sn, cn,
765 r, r->request.tag, r->request.fid, r->request.type,
766 r->reply.type, r->reply.tag);
772 mountio(Mnt *m, Mntrpc *r)
779 if(strcmp(up->errstr, Eintr) != 0){
783 r = mntflushalloc(r, m->msize);
792 /* Transmit a file system rpc */
795 n = convS2M(&r->request, r->rpc, m->msize);
797 panic("bad message type in mountio");
798 if(devtab[m->c->type]->write(m->c, r->rpc, n, 0) != n)
800 r->stime = fastticks(nil);
803 /* Gate readers onto the mount point one at a time */
809 sleep(&r->r, rpcattn, r);
818 while(r->done == 0) {
819 if(mntrpcread(m, r) < 0)
829 doread(Mnt *m, int len)
833 while(qlen(m->q) < len){
834 b = devtab[m->c->type]->bread(m->c, m->msize, 0);
837 if(blocklen(b) == 0){
847 mntrpcread(Mnt *m, Mntrpc *r)
855 /* read at least length, type, and tag and pullup to a single block */
856 if(doread(m, BIT32SZ+BIT8SZ+BIT16SZ) < 0)
858 nb = pullupqueue(m->q, BIT32SZ+BIT8SZ+BIT16SZ);
860 /* read in the rest of the message, avoid ridiculous (for now) message sizes */
861 len = GBIT32(nb->rp);
863 qdiscard(m->q, qlen(m->q));
866 if(doread(m, len) < 0)
869 /* pullup the header (i.e. everything except data) */
873 hlen = BIT32SZ+BIT8SZ+BIT16SZ+BIT32SZ;
879 nb = pullupqueue(m->q, hlen);
881 if(convM2S(nb->rp, len, &r->reply) <= 0){
882 /* bad message, dump it */
883 print("mntrpcread: convM2S failed\n");
888 /* hang the data off of the fcall struct */
904 /* split block and put unused bit back */
906 memmove(nb->wp, b->rp+len, i-len);
925 for(q = m->queue; q; q = q->list) {
934 mountmux(Mnt *m, Mntrpc *r)
940 for(q = *l; q; q = q->list) {
941 /* look for a reply to a message */
942 if(q->request.tag == r->reply.tag) {
946 * Completed someone else.
947 * Trade pointers to receive buffer.
956 (*mntstats)(q->request.type,
958 q->reqlen + r->replen);
966 print("unexpected reply tag %ud; type %d\n", r->reply.tag, r->reply.type);
970 * Create a new flush request and chain the previous
974 mntflushalloc(Mntrpc *r, ulong iounit)
978 fr = mntralloc(0, iounit);
980 fr->request.type = Tflush;
981 if(r->request.type == Tflush)
982 fr->request.oldtag = r->request.oldtag;
984 fr->request.oldtag = r->request.tag;
991 * Free a chain of flushes. Remove each unanswered
992 * flush and the original message from the unanswered
993 * request queue. Mark the original message as done
994 * and if it hasn't been answered set the reply to to
998 mntflushfree(Mnt *m, Mntrpc *r)
1005 r->reply.type = Rflush;
1020 for(i = 0; i < NMASK; i++){
1021 v = mntalloc.tagmask[i];
1024 for(j = 0; j < 1<<TAGSHIFT; j++)
1025 if((v & (1<<j)) == 0){
1026 mntalloc.tagmask[i] |= 1<<j;
1027 return (i<<TAGSHIFT) + j;
1030 panic("no friggin tags left");
1037 mntalloc.tagmask[t>>TAGSHIFT] &= ~(1<<(t&TAGMASK));
1041 mntralloc(Chan *c, ulong msize)
1046 new = mntalloc.rpcfree;
1048 new = malloc(sizeof(Mntrpc));
1051 exhausted("mount rpc header");
1054 * The header is split from the data buffer as
1055 * mountmux may swap the buffer with another header.
1057 new->rpc = mallocz(msize, 0);
1058 if(new->rpc == nil){
1061 exhausted("mount rpc buffer");
1063 new->rpclen = msize;
1064 new->request.tag = alloctag();
1067 mntalloc.rpcfree = new->list;
1068 mntalloc.nrpcfree--;
1069 if(new->rpclen < msize){
1071 new->rpc = mallocz(msize, 0);
1072 if(new->rpc == nil){
1074 mntalloc.nrpcused--;
1076 exhausted("mount rpc buffer");
1078 new->rpclen = msize;
1081 mntalloc.nrpcused++;
1096 if(mntalloc.nrpcfree >= 10){
1098 freetag(r->request.tag);
1102 r->list = mntalloc.rpcfree;
1103 mntalloc.rpcfree = r;
1104 mntalloc.nrpcfree++;
1106 mntalloc.nrpcused--;
1111 mntqrm(Mnt *m, Mntrpc *r)
1119 for(f = *l; f; f = f->list) {
1134 /* This routine is mostly vestiges of prior lives; now it's just sanity checking */
1137 panic("mntchk 1: nil mchan c %s", chanpath(c));
1142 print("mntchk 2: nil mux c %s c->mchan %s \n", chanpath(c), chanpath(c->mchan));
1145 * Was it closed and reused (was error(Eshutdown); now, it cannot happen)
1147 if(m->id == 0 || m->id >= c->dev)
1148 panic("mntchk 3: can't happen");
1154 * Rewrite channel type and dev for in-flight data to
1155 * reflect local values. These entries are known to be
1156 * the first two in the Dir encoding after the count.
1159 mntdirfix(uchar *dirbuf, Chan *c)
1163 r = devtab[c->type]->dc;
1164 dirbuf += BIT16SZ; /* skip count */
1167 PBIT32(dirbuf, c->dev);
1176 return r->done || r->m->rip == 0;