15 typedef struct Dirtab Dirtab;
16 typedef struct Fid Fid;
17 typedef struct Holdq Holdq;
18 typedef struct Readreq Readreq;
19 typedef struct Sendreq Sendreq;
27 int nopen; /* #fids open on this port */
42 long offset; /* zeroed at beginning of each message, read or write */
43 char *writebuf; /* partial message written so far; offset tells how much */
58 int nfid; /* number of fids that should receive this message */
59 int nleft; /* number left that haven't received it */
60 Fid **fid; /* fid[nfid] */
62 char *pack; /* plumbpack()ed message */
63 int npack; /* length of pack */
73 struct /* needed because incref() doesn't return value */
91 static Dirtab dir[NDIR] =
93 { ".", QTDIR, Qdir, 0500|DMDIR },
94 { "rules", QTFILE, Qrules, 0600 },
95 { "send", QTFILE, Qsend, 0200 },
97 static int ndir = NQID;
101 static Fid *fids[Nhash];
102 static QLock readlock;
104 static int messagesize = 8192+IOHDRSZ; /* good start */
106 static void fsysproc(void*);
107 static void fsysrespond(Fcall*, uchar*, char*);
108 static Fid* newfid(int);
110 static Fcall* fsysflush(Fcall*, uchar*, Fid*);
111 static Fcall* fsysversion(Fcall*, uchar*, Fid*);
112 static Fcall* fsysauth(Fcall*, uchar*, Fid*);
113 static Fcall* fsysattach(Fcall*, uchar*, Fid*);
114 static Fcall* fsyswalk(Fcall*, uchar*, Fid*);
115 static Fcall* fsysopen(Fcall*, uchar*, Fid*);
116 static Fcall* fsyscreate(Fcall*, uchar*, Fid*);
117 static Fcall* fsysread(Fcall*, uchar*, Fid*);
118 static Fcall* fsyswrite(Fcall*, uchar*, Fid*);
119 static Fcall* fsysclunk(Fcall*, uchar*, Fid*);
120 static Fcall* fsysremove(Fcall*, uchar*, Fid*);
121 static Fcall* fsysstat(Fcall*, uchar*, Fid*);
122 static Fcall* fsyswstat(Fcall*, uchar*, Fid*);
124 Fcall* (*fcall[Tmax])(Fcall*, uchar*, Fid*) =
126 [Tflush] = fsysflush,
127 [Tversion] = fsysversion,
129 [Tattach] = fsysattach,
132 [Tcreate] = fsyscreate,
134 [Twrite] = fsyswrite,
135 [Tclunk] = fsysclunk,
136 [Tremove]= fsysremove,
138 [Twstat] = fsyswstat,
141 char Ebadfcall[] = "bad fcall type";
142 char Eperm[] = "permission denied";
143 char Enomem[] = "malloc failed for buffer";
144 char Enotdir[] = "not a directory";
145 char Enoexist[] = "plumb file does not exist";
146 char Eisdir[] = "file is a directory";
147 char Ebadmsg[] = "bad plumb message format";
148 char Enosuchport[] ="no such plumb port";
149 char Enoport[] = "couldn't find destination for message";
150 char Einuse[] = "file already open";
153 * Add new port. A no-op if port already exists or is the null string
162 for(i=NQID; i<ndir; i++)
163 if(strcmp(port, dir[i].name) == 0)
166 fprint(2, "plumb: too many ports; max %d\n", NDIR);
170 dir[i].name = estrdup(port);
174 ports = erealloc(ports, nports*sizeof(char*));
175 ports[nports-1] = dir[i].name;
189 fmtinstall('F', fcallfmt);
192 error("can't create pipe: %r");
193 /* 0 will be server end, 1 will be client end */
195 if(post9pservice(p[1], "plumb") < 0)
196 sysfatal("post9pservice plumb: %r");
198 threadcreate(fsysproc, nil, Stack);
212 buf = malloc(messagesize); /* avoid memset of emalloc */
214 error("malloc failed: %r");
216 n = threadread9pmsg(srvfd, buf, messagesize);
219 error("i/o error on server channel");
220 threadexitsall("unmounted");
222 if(readlock.head == nil) /* no other processes waiting to read; start one */
223 threadcreate(fsysproc, nil, Stack);
226 t = emalloc(sizeof(Fcall));
227 if(convM2S(buf, n, t) != n)
228 error("convert error in convM2S");
230 fprint(2, "<= %F\n", t);
231 if(fcall[t->type] == nil)
232 fsysrespond(t, buf, Ebadfcall);
234 if(t->type==Tversion || t->type==Tauth)
238 t = (*fcall[t->type])(t, buf, f);
244 fsysrespond(Fcall *t, uchar *buf, char *err)
254 buf = emalloc(messagesize);
255 n = convS2M(t, buf, messagesize);
257 error("convert error in convS2M");
258 if(write(srvfd, buf, n) != n)
259 error("write error in respond");
261 fprint(2, "=> %F\n", t);
273 fh = &fids[fid&(Nhash-1)];
274 for(f=*fh; f; f=f->next)
277 else if(ff==nil && !f->busy)
284 f = emalloc(sizeof *f);
294 dostat(Dirtab *dir, uchar *buf, uint nbuf, uint clock)
298 d.qid.type = dir->type;
299 d.qid.path = dir->qid;
302 d.length = 0; /* would be nice to do better */
309 return convD2M(&d, buf, nbuf);
313 queuesend(Dirtab *d, Plumbmsg *m)
319 s = emalloc(sizeof(Sendreq));
322 s->fid = emalloc(s->nfid*sizeof(Fid*));
324 /* build array of fids open on this channel */
325 for(f=d->fopen; f!=nil; f=f->nextopen)
329 /* link to end of queue; drainqueue() searches in sender order so this implements a FIFO */
330 for(t=d->sendq; t!=nil; t=t->next)
340 queueread(Dirtab *d, Fcall *t, uchar *buf, Fid *f)
344 r = emalloc(sizeof(Readreq));
353 drainqueue(Dirtab *d)
355 Readreq *r, *nextr, *prevr;
356 Sendreq *s, *nexts, *prevs;
360 for(s=d->sendq; s!=nil; s=nexts){
362 for(i=0; i<s->nfid; i++){
364 for(r=d->readq; r!=nil; r=nextr){
366 if(r->fid == s->fid[i]){
367 /* pack the message if necessary */
369 s->pack = plumbpack(s->msg, &s->npack);
370 /* exchange the stuff... */
371 r->fcall->data = s->pack+r->fid->offset;
372 n = s->npack - r->fid->offset;
373 if(n > messagesize-IOHDRSZ)
374 n = messagesize-IOHDRSZ;
375 if(n > r->fcall->count)
378 fsysrespond(r->fcall, r->buf, nil);
380 if(r->fid->offset >= s->npack){
381 /* message transferred; delete this fid from send queue */
386 /* delete read request from queue */
388 prevr->next = r->next;
398 /* if no fids left, delete this send from queue */
404 prevs->next = s->next;
413 /* can't flush a send because they are always answered synchronously */
415 flushqueue(Dirtab *d, int oldtag)
420 for(r=d->readq; r!=nil; r=r->next){
421 if(oldtag == r->fcall->tag){
422 /* delete read request from queue */
424 prevr->next = r->next;
436 /* remove messages awaiting delivery to now-closing fid */
438 removesenders(Dirtab *d, Fid *fid)
440 Sendreq *s, *nexts, *prevs;
444 for(s=d->sendq; s!=nil; s=nexts){
446 for(i=0; i<s->nfid; i++)
447 if(fid == s->fid[i]){
448 /* delete this fid from send queue */
453 /* if no fids left, delete this send from queue */
459 prevs->next = s->next;
469 hold(Plumbmsg *m, Dirtab *d)
473 h = emalloc(sizeof(Holdq));
475 /* add to end of queue */
479 for(q=d->holdq; q->next!=nil; q=q->next)
490 while(d->holdq != nil){
493 queuesend(d, h->msg);
494 /* no need to drain queue because we know no-one is reading yet */
500 dispose(Fcall *t, uchar *buf, Plumbmsg *m, Ruleset *rs, Exec *e)
507 if(m->dst==nil || m->dst[0]=='\0'){
510 err = startup(rs, e);
513 for(i=NQID; i<ndir; i++)
514 if(strcmp(m->dst, dir[i].name) == 0){
515 if(dir[i].nopen == 0){
516 err = startup(rs, e);
517 if(e!=nil && e->holdforclient)
522 queuesend(&dir[i], m);
529 fsysrespond(t, buf, err);
534 fsysversion(Fcall *t, uchar *buf, Fid *fid)
539 fsysrespond(t, buf, "version: message size too small");
542 if(t->msize < messagesize)
543 messagesize = t->msize;
544 t->msize = messagesize;
545 if(strncmp(t->version, "9P2000", 6) != 0){
546 fsysrespond(t, buf, "unrecognized 9P version");
549 t->version = "9P2000";
550 fsysrespond(t, buf, nil);
555 fsysauth(Fcall *t, uchar *buf, Fid *fid)
558 fsysrespond(t, buf, "plumber: authentication not required");
563 fsysattach(Fcall *t, uchar *buf, Fid *f)
567 if(strcmp(t->uname, user) != 0){
568 fsysrespond(&out, buf, Eperm);
577 memset(&out, 0, sizeof(Fcall));
582 fsysrespond(&out, buf, nil);
587 fsysflush(Fcall *t, uchar *buf, Fid *fid)
593 for(i=NQID; i<ndir; i++)
594 flushqueue(&dir[i], t->oldtag);
596 fsysrespond(t, buf, nil);
601 fsyswalk(Fcall *t, uchar *buf, Fid *f)
613 fsysrespond(t, buf, "clone of an open fid");
618 if(t->fid != t->newfid){
619 nf = newfid(t->newfid);
621 fsysrespond(t, buf, "clone to a busy fid");
637 for(i=0; i<t->nwname; i++){
638 if((q.type & QTDIR) == 0){
642 if(strcmp(t->wname[i], "..") == 0){
649 out.wqid[out.nwqid++] = q;
655 if(strcmp(t->wname[i], d->name) == 0){
668 if(err!=nil || out.nwqid<t->nwname){
671 }else if(out.nwqid == t->nwname){
676 fsysrespond(&out, buf, err);
681 fsysopen(Fcall *t, uchar *buf, Fid *f)
683 int m, clearrules, mode;
686 if(t->mode & OTRUNC){
687 if(f->qid.path != Qrules)
691 /* can't truncate anything, so just disregard */
692 mode = t->mode & ~(OTRUNC|OCEXEC);
693 /* can't execute or remove anything */
694 if(mode==OEXEC || (mode&ORCLOSE))
709 if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
711 if(f->qid.path==Qrules && (mode==OWRITE || mode==ORDWR)){
713 if(rulesref.ref++ != 0){
715 unlock(&rulesref.lk);
716 fsysrespond(t, buf, Einuse);
719 unlock(&rulesref.lk);
731 f->nextopen = f->dir->fopen;
735 fsysrespond(t, buf, nil);
739 fsysrespond(t, buf, Eperm);
744 fsyscreate(Fcall *t, uchar *buf, Fid *fid)
747 fsysrespond(t, buf, Eperm);
752 fsysreadrules(Fcall *t, uchar *buf)
763 t->data = p+t->offset;
764 if(t->offset+t->count > n)
765 t->count = n-t->offset;
767 fsysrespond(t, buf, nil);
773 fsysread(Fcall *t, uchar *buf, Fid *f)
781 if(f->qid.path != Qdir){
782 if(f->qid.path == Qrules)
783 return fsysreadrules(t, buf);
785 if(f->qid.path < NQID){
786 fsysrespond(t, buf, "internal error: unknown read port");
790 queueread(f->dir, t, buf, f);
796 e = t->offset+t->count;
798 b = malloc(messagesize-IOHDRSZ);
800 fsysrespond(t, buf, Enomem);
805 d++; /* first entry is '.' */
806 for(i=0; d->name!=nil && i<e; i+=len){
807 len = dostat(d, b+n, messagesize-IOHDRSZ-n, clock);
816 fsysrespond(t, buf, nil);
822 fsyswrite(Fcall *t, uchar *buf, Fid *f)
830 switch((int)f->qid.path){
832 fsysrespond(t, buf, Eisdir);
836 fsysrespond(t, buf, writerules(t->data, t->count));
843 /* partial message already assembled */
844 f->writebuf = erealloc(f->writebuf, f->offset + t->count);
845 memmove(f->writebuf+f->offset, t->data, t->count);
847 count = f->offset+t->count;
849 m = plumbunpackpartial(data, count, &n);
855 fsysrespond(t, buf, Ebadmsg);
858 /* can read more... */
860 f->writebuf = emalloc(t->count);
861 memmove(f->writebuf, t->data, t->count);
863 /* else buffer has already been grown */
864 f->offset += t->count;
865 fsysrespond(t, buf, nil);
868 /* release partial buffer */
872 for(i=0; rules[i]; i++)
873 if((e=matchruleset(m, rules[i])) != nil){
874 dispose(t, buf, m, rules[i], e);
878 dispose(t, buf, m, nil, nil);
881 fsysrespond(t, buf, "no matching plumb rule");
884 fsysrespond(t, buf, "internal error: write to unknown file");
889 fsysstat(Fcall *t, uchar *buf, Fid *f)
891 t->stat = emalloc(messagesize-IOHDRSZ);
892 t->nstat = dostat(f->dir, t->stat, messagesize-IOHDRSZ, clock);
893 fsysrespond(t, buf, nil);
900 fsyswstat(Fcall *t, uchar *buf, Fid *fid)
903 fsysrespond(t, buf, Eperm);
908 fsysremove(Fcall *t, uchar *buf, Fid *fid)
911 fsysrespond(t, buf, Eperm);
916 fsysclunk(Fcall *t, uchar *buf, Fid *f)
925 if(d->qid==Qrules && (f->mode==OWRITE || f->mode==ORDWR)){
927 * just to be sure last rule is parsed; error messages will be lost, though,
928 * unless last write ended with a blank line
933 unlock(&rulesref.lk);
936 for(p=d->fopen; p; p=p->nextopen){
939 prev->nextopen = f->nextopen;
941 d->fopen = f->nextopen;
951 if(f->writebuf != nil){
956 fsysrespond(t, buf, nil);