9 /* static char Ebadattach[] = "unknown specifier in attach"; */
10 static char Ebadoffset[] = "bad offset";
11 /* static char Ebadcount[] = "bad count"; */
12 static char Ebotch[] = "9P protocol botch";
13 static char Ecreatenondir[] = "create in non-directory";
14 static char Edupfid[] = "duplicate fid";
15 static char Eduptag[] = "duplicate tag";
16 static char Eisdir[] = "is a directory";
17 static char Enocreate[] = "create prohibited";
18 /* static char Enomem[] = "out of memory"; */
19 static char Enoremove[] = "remove prohibited";
20 static char Enostat[] = "stat prohibited";
21 static char Enotfound[] = "file not found";
22 /* static char Enowrite[] = "write prohibited"; */
23 static char Enowstat[] = "wstat prohibited";
24 static char Eperm[] = "permission denied";
25 static char Eunknownfid[] = "unknown fid";
26 static char Ebaddir[] = "bad directory in wstat";
27 static char Ewalknodir[] = "walk in non-directory";
30 setfcallerror(Fcall *f, char *err)
37 changemsize(Srv *srv, int msize)
39 if(srv->rbuf && srv->wbuf && srv->msize == msize)
46 srv->rbuf = emalloc9p(msize);
47 srv->wbuf = emalloc9p(msize);
61 if((n = read9pmsg(s->infd, s->rbuf, s->msize)) <= 0){
66 buf = emalloc9p(n+1); /* +1 for NUL in swrite */
67 memmove(buf, s->rbuf, n);
70 if(convM2Su(buf, n, &f, s->dotu) != n){
75 if((r=allocreq(s->rpool, f.tag)) == nil){ /* duplicate tag: cons up a fake Req */
76 r = emalloc9p(sizeof *r);
87 fprint(2, "<-%d- %F: dup tag\n", s->infd, &f);
95 memset(&r->ofcall, 0, sizeof r->ofcall);
96 r->type = r->ifcall.type;
100 fprint(2, "<-%d- %F: %s\n", s->infd, &r->ifcall, r->error);
102 fprint(2, "<-%d- %F\n", s->infd, &r->ifcall);
117 for(i=0; i<r->ifcall.nwname; i++)
118 if(f = walkfile(f, r->ifcall.wname[i]))
119 r->ofcall.wqid[i] = f->dir.qid;
126 r->newfid->qid = r->newfid->file->dir.qid;
132 walkandclone(Req *r, char *(*walk1)(Fid*, char*, void*), char *(*clone)(Fid*, Fid*, void*), void *arg)
137 if(r->fid == r->newfid && r->ifcall.nwname > 1){
138 respond(r, "lib9p: unused documented feature not implemented");
142 if(r->fid != r->newfid){
143 r->newfid->qid = r->fid->qid;
144 if(clone && (e = clone(r->fid, r->newfid, arg))){
151 for(i=0; i<r->ifcall.nwname; i++){
152 if(e = walk1(r->newfid, r->ifcall.wname[i], arg))
154 r->ofcall.wqid[i] = r->newfid->qid;
165 sversion(Srv *srv, Req *r)
169 if(strncmp(r->ifcall.version, "9P", 2) != 0){
170 r->ofcall.version = "unknown";
175 if(strncmp(r->ifcall.version, "9P2000.u", 8) == 0){
176 r->ofcall.version = "9P2000.u";
179 r->ofcall.version = "9P2000";
182 r->ofcall.msize = r->ifcall.msize;
186 rversion(Req *r, char *error)
188 assert(error == nil);
189 changemsize(r->srv, r->ofcall.msize);
193 sauth(Srv *srv, Req *r)
197 if((r->afid = allocfid(srv->fpool, r->ifcall.afid)) == nil){
204 snprint(e, sizeof e, "%s: authentication not required", argv0);
209 rauth(Req *r, char *error)
212 closefid(removefid(r->srv->fpool, r->afid->fid));
216 sattach(Srv *srv, Req *r)
218 if((r->fid = allocfid(srv->fpool, r->ifcall.fid)) == nil){
223 if(r->ifcall.afid != NOFID && (r->afid = lookupfid(srv->fpool, r->ifcall.afid)) == nil){
224 respond(r, Eunknownfid);
227 r->fid->uid = estrdup9p(r->ifcall.uname);
229 r->fid->file = srv->tree->root;
230 incref(&r->fid->file->ref);
231 r->ofcall.qid = r->fid->file->dir.qid;
232 r->fid->qid = r->ofcall.qid;
241 rattach(Req *r, char *error)
244 closefid(removefid(r->srv->fpool, r->fid->fid));
248 sflush(Srv *srv, Req *r)
250 r->oldreq = lookupreq(srv->rpool, r->ifcall.oldtag);
251 if(r->oldreq == nil || r->oldreq == r)
259 rflush(Req *r, char *error)
263 assert(error == nil);
267 if(or->responded == 0){
268 or->flush = erealloc9p(or->flush, (or->nflush+1)*sizeof(or->flush[0]));
269 or->flush[or->nflush++] = r;
271 return -1; /* delay response until or is responded */
281 oldwalk1(Fid *fid, char *name, void *arg)
288 e = srv->walk1(fid, name, &qid);
296 oldclone(Fid *fid, Fid *newfid, void *arg)
301 if(srv->clone == nil)
303 return srv->clone(fid, newfid);
307 swalk(Srv *srv, Req *r)
309 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
310 respond(r, Eunknownfid);
313 if(r->fid->omode != -1){
314 respond(r, "cannot clone open fid");
317 if(r->ifcall.nwname && !(r->fid->qid.type&QTDIR)){
318 respond(r, Ewalknodir);
321 if(r->ifcall.fid != r->ifcall.newfid){
322 if((r->newfid = allocfid(srv->fpool, r->ifcall.newfid)) == nil){
326 r->newfid->uid = estrdup9p(r->fid->uid);
328 incref(&r->fid->ref);
334 walkandclone(r, oldwalk1, oldclone, srv);
338 sysfatal("no walk function, no file trees");
341 rwalk(Req *r, char *error)
343 if(error || r->ofcall.nwqid < r->ifcall.nwname){
344 if(r->ifcall.fid != r->ifcall.newfid && r->newfid)
345 closefid(removefid(r->srv->fpool, r->newfid->fid));
346 if (r->ofcall.nwqid==0){
347 if(error==nil && r->ifcall.nwname!=0)
348 r->error = Enotfound;
350 r->error = nil; /* No error on partial walks */
352 if(r->ofcall.nwqid == 0){
354 r->newfid->qid = r->fid->qid;
356 /* if file trees are in use, filewalk took care of the rest */
357 r->newfid->qid = r->ofcall.wqid[r->ofcall.nwqid-1];
363 sopen(Srv *srv, Req *r)
367 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
368 respond(r, Eunknownfid);
371 if(r->fid->omode != -1){
375 if((r->fid->qid.type&QTDIR) && (r->ifcall.mode&~ORCLOSE) != OREAD){
379 r->ofcall.qid = r->fid->qid;
380 switch(r->ifcall.mode&3){
396 if(r->ifcall.mode&OTRUNC)
398 if((r->fid->qid.type&QTDIR) && p!=AREAD){
403 if(!hasperm(r->fid->file, r->fid->uid, p)){
408 if((r->ifcall.mode&ORCLOSE)
409 && !hasperm(r->fid->file->parent, r->fid->uid, AWRITE)){
413 r->ofcall.qid = r->fid->file->dir.qid;
414 if((r->ofcall.qid.type&QTDIR)
415 && (r->fid->rdir = opendirfile(r->fid->file)) == nil){
416 respond(r, "opendirfile failed");
426 ropen(Req *r, char *error)
432 snprint(errbuf, sizeof errbuf, "fid mode is 0x%ux\n", r->ifcall.mode);
433 write(2, errbuf, strlen(errbuf));
435 r->fid->omode = r->ifcall.mode;
436 r->fid->qid = r->ofcall.qid;
437 if(r->ofcall.qid.type&QTDIR)
438 r->fid->diroffset = 0;
442 screate(Srv *srv, Req *r)
444 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil)
445 respond(r, Eunknownfid);
446 else if(r->fid->omode != -1)
448 else if(!(r->fid->qid.type&QTDIR))
449 respond(r, Ecreatenondir);
450 else if(r->fid->file && !hasperm(r->fid->file, r->fid->uid, AWRITE))
455 respond(r, Enocreate);
458 rcreate(Req *r, char *error)
462 r->fid->omode = r->ifcall.mode;
463 r->fid->qid = r->ofcall.qid;
467 sread(Srv *srv, Req *r)
471 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
472 respond(r, Eunknownfid);
475 if(r->ifcall.count < 0){
479 if(r->ifcall.offset < 0
480 || ((r->fid->qid.type&QTDIR) && r->ifcall.offset != 0 && r->ifcall.offset != r->fid->diroffset)){
481 respond(r, Ebadoffset);
485 if(r->ifcall.count > srv->msize - IOHDRSZ)
486 r->ifcall.count = srv->msize - IOHDRSZ;
487 r->rbuf = emalloc9p(r->ifcall.count);
488 r->ofcall.data = r->rbuf;
489 o = r->fid->omode & 3;
490 if(o != OREAD && o != ORDWR && o != OEXEC){
494 if((r->fid->qid.type&QTDIR) && r->fid->file){
495 r->ofcall.count = readdirfile(r->fid->rdir, r->rbuf, r->ifcall.count);
502 respond(r, "no srv->read");
505 rread(Req *r, char *error)
507 if(error==nil && (r->fid->qid.type&QTDIR))
508 r->fid->diroffset = r->ifcall.offset + r->ofcall.count;
512 swrite(Srv *srv, Req *r)
517 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
518 respond(r, Eunknownfid);
521 if(r->ifcall.count < 0){
525 if(r->ifcall.offset < 0){
529 if(r->ifcall.count > srv->msize - IOHDRSZ)
530 r->ifcall.count = srv->msize - IOHDRSZ;
531 o = r->fid->omode & 3;
532 if(o != OWRITE && o != ORDWR){
533 snprint(e, sizeof e, "write on fid with open mode 0x%ux", r->fid->omode);
538 r->ifcall.data[r->ifcall.count] = 0; /* enough room - see getreq */
541 respond(r, "no srv->write");
544 rwrite(Req *r, char *error)
549 r->fid->file->dir.qid.vers++;
553 sclunk(Srv *srv, Req *r)
555 if((r->fid = removefid(srv->fpool, r->ifcall.fid)) == nil)
556 respond(r, Eunknownfid);
561 rclunk(Req *r, char *msg)
568 sremove(Srv *srv, Req *r)
570 if((r->fid = removefid(srv->fpool, r->ifcall.fid)) == nil){
571 respond(r, Eunknownfid);
575 if(r->fid->file && !hasperm(r->fid->file->parent, r->fid->uid, AWRITE)){
582 respond(r, r->fid->file ? nil : Enoremove);
585 rremove(Req *r, char *error, char *errbuf)
590 if(removefile(r->fid->file) < 0){
591 snprint(errbuf, ERRMAX, "remove %s: %r",
592 r->fid->file->dir.name);
600 sstat(Srv *srv, Req *r)
602 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
603 respond(r, Eunknownfid);
607 r->d = r->fid->file->dir;
609 r->d.name = estrdup9p(r->d.name);
611 r->d.uid = estrdup9p(r->d.uid);
613 r->d.gid = estrdup9p(r->d.gid);
615 r->d.muid = estrdup9p(r->d.muid);
619 else if(r->fid->file)
625 rstat(Req *r, char *error)
633 if(convD2Mu(&r->d, tmp, BIT16SZ, r->srv->dotu) != BIT16SZ){
634 r->error = "convD2Mu(_,_,BIT16SZ,_) did not return BIT16SZ";
637 n = GBIT16(tmp)+BIT16SZ;
638 statbuf = emalloc9p(n);
640 r->error = "out of memory";
643 r->ofcall.nstat = convD2Mu(&r->d, statbuf, n, r->srv->dotu);
644 r->ofcall.stat = statbuf; /* freed in closereq */
645 if(r->ofcall.nstat <= BIT16SZ){
646 r->error = "convD2Mu fails";
653 swstat(Srv *srv, Req *r)
655 if((r->fid = lookupfid(srv->fpool, r->ifcall.fid)) == nil){
656 respond(r, Eunknownfid);
659 if(srv->wstat == nil){
660 respond(r, Enowstat);
663 if(convM2Du(r->ifcall.stat, r->ifcall.nstat, &r->d, (char*)r->ifcall.stat, srv->dotu) != r->ifcall.nstat){
667 if((ushort)~r->d.type){
668 respond(r, "wstat -- attempt to change type");
672 respond(r, "wstat -- attempt to change dev");
675 if((uchar)~r->d.qid.type || (ulong)~r->d.qid.vers || (uvlong)~r->d.qid.path){
676 respond(r, "wstat -- attempt to change qid");
679 if(r->d.muid && r->d.muid[0]){
680 respond(r, "wstat -- attempt to change muid");
683 if((ulong)~r->d.mode && ((r->d.mode&DMDIR)>>24) != (r->fid->qid.type&QTDIR)){
684 respond(r, "wstat -- attempt to change DMDIR bit");
690 rwstat(Req *r, char *msg)
701 fmtinstall('D', dirfmt);
702 fmtinstall('F', fcallfmt);
704 if(srv->fpool == nil)
705 srv->fpool = allocfidpool(srv->destroyfid);
706 if(srv->rpool == nil)
707 srv->rpool = allocreqpool(srv->destroyreq);
709 srv->msize = 8192+IOHDRSZ;
711 changemsize(srv, srv->msize);
713 srv->fpool->srv = srv;
714 srv->rpool->srv = srv;
719 while(r = getreq(srv)){
721 respond(r, r->error);
724 switch(r->ifcall.type){
726 respond(r, "unknown message");
728 case Tversion: sversion(srv, r); break;
729 case Tauth: sauth(srv, r); break;
730 case Tattach: sattach(srv, r); break;
731 case Tflush: sflush(srv, r); break;
732 case Twalk: swalk(srv, r); break;
733 case Topen: sopen(srv, r); break;
734 case Tcreate: screate(srv, r); break;
735 case Tread: sread(srv, r); break;
736 case Twrite: swrite(srv, r); break;
737 case Tclunk: sclunk(srv, r); break;
738 case Tremove: sremove(srv, r); break;
739 case Tstat: sstat(srv, r); break;
740 case Twstat: swstat(srv, r); break;
749 respond(Req *r, char *error)
763 assert(r->responded == 0);
766 switch(r->ifcall.type){
770 * Flush is special. If the handler says so, we return
771 * without further processing. Respond will be called
772 * again once it is safe.
775 if(rflush(r, error)<0)
778 case Tversion: rversion(r, error); break;
779 case Tauth: rauth(r, error); break;
780 case Tattach: rattach(r, error); break;
781 case Twalk: rwalk(r, error); break;
782 case Topen: ropen(r, error); break;
783 case Tcreate: rcreate(r, error); break;
784 case Tread: rread(r, error); break;
785 case Twrite: rwrite(r, error); break;
786 case Tclunk: rclunk(r, error); break;
787 case Tremove: rremove(r, error, errbuf); break;
788 case Tstat: rstat(r, error); break;
789 case Twstat: rwstat(r, error); break;
792 r->ofcall.tag = r->ifcall.tag;
793 r->ofcall.type = r->ifcall.type+1;
795 setfcallerror(&r->ofcall, r->error);
798 fprint(2, "-%d-> %F\n", srv->outfd, &r->ofcall);
801 n = convS2Mu(&r->ofcall, srv->wbuf, srv->msize, srv->dotu);
803 fprint(2, "n = %d %F\n", n, &r->ofcall);
808 * There is a race here - we must remove the entry before
809 * the write, so that if the client is very fast and reuses the
810 * tag, the read loop won't think it is still in use.
812 * By removing the entry before the write, we open up a
813 * race with incoming Tflush messages. Specifically, an
814 * incoming Tflush might not see r even though it has not
815 * yet been responded to. It would then send an Rflush
816 * immediately, potentially before we do the write. This can't
817 * happen because we already old srv->wlock, so nothing
818 * is going out on the wire before this write.
820 if(r->pool) /* not a fake */
821 closereq(removereq(r->pool, r->ifcall.tag));
826 if(r->ref.ref == 1+r->nflush)
829 * There are no references other than in our r->flush array,
830 * so no one else should be accessing r concurrently.
831 * Close the fid now, before responding to the message.
833 * If the client is behaving (there are no outstanding T-messages
834 * that reference r->fid) and the message is a Tclunk or Tremove,
835 * then this closefid will call destroyfid.
837 * This means destroyfid can't piddle around
838 * indefinitely (we're holding srv->wlock!), but it provides
839 * for tighter semantics as to when destroyfid is called.
841 * LANL has observed cases where waiting until after the write
842 * can delay a closefid on a Twrite for many 9P transactions,
843 * so that a handful of transactions can happen including a Tclunk
844 * and a Topen, and the original fid will still not be destroyed.
850 m = write(srv->outfd, srv->wbuf, n);
852 sysfatal("lib9p srv: write %d returned %d on fd %d: %r", n, m, srv->outfd);
853 qunlock(&srv->wlock);
856 qlock(&r->lk); /* no one will add flushes now */
858 for(i=0; i<r->nflush; i++){
859 r->flush[i]->oldreq = nil; /* so it doesn't try to lock us! */
860 respond(r->flush[i], nil);
874 postfd(char *name, int pfd)
879 snprint(buf, sizeof buf, "/srv/%s", name);
881 fprint(2, "postfd %s\n", buf);
882 fd = create(buf, OWRITE|ORCLOSE|OCEXEC, 0600);
885 fprint(2, "create fails: %r\n");
888 if(fprint(fd, "%d", pfd) < 0){
890 fprint(2, "write fails: %r\n");
895 fprint(2, "postfd successful\n");