22 static Fid *fids[Nhash];
26 static Xfid* fsysflush(Xfid*, Fid*);
27 static Xfid* fsysauth(Xfid*, Fid*);
28 static Xfid* fsysversion(Xfid*, Fid*);
29 static Xfid* fsysattach(Xfid*, Fid*);
30 static Xfid* fsyswalk(Xfid*, Fid*);
31 static Xfid* fsysopen(Xfid*, Fid*);
32 static Xfid* fsyscreate(Xfid*, Fid*);
33 static Xfid* fsysread(Xfid*, Fid*);
34 static Xfid* fsyswrite(Xfid*, Fid*);
35 static Xfid* fsysclunk(Xfid*, Fid*);
36 static Xfid* fsysremove(Xfid*, Fid*);
37 static Xfid* fsysstat(Xfid*, Fid*);
38 static Xfid* fsyswstat(Xfid*, Fid*);
40 Xfid* (*fcall[Tmax])(Xfid*, Fid*);
45 fcall[Tflush] = fsysflush;
46 fcall[Tversion] = fsysversion;
47 fcall[Tauth] = fsysauth;
48 fcall[Tattach] = fsysattach;
49 fcall[Twalk] = fsyswalk;
50 fcall[Topen] = fsysopen;
51 fcall[Tcreate] = fsyscreate;
52 fcall[Tread] = fsysread;
53 fcall[Twrite] = fsyswrite;
54 fcall[Tclunk] = fsysclunk;
55 fcall[Tremove]= fsysremove;
56 fcall[Tstat] = fsysstat;
57 fcall[Twstat] = fsyswstat;
60 char Eperm[] = "permission denied";
61 char Eexist[] = "file does not exist";
62 char Enotdir[] = "not a directory";
66 { ".", QTDIR, Qdir, 0500|DMDIR },
67 { "acme", QTDIR, Qacme, 0500|DMDIR },
68 { "cons", QTFILE, Qcons, 0600 },
69 { "consctl", QTFILE, Qconsctl, 0000 },
70 { "draw", QTDIR, Qdraw, 0000|DMDIR }, /* to suppress graphics progs started in acme */
71 { "editout", QTFILE, Qeditout, 0200 },
72 { "index", QTFILE, Qindex, 0400 },
73 { "label", QTFILE, Qlabel, 0600 },
74 { "new", QTDIR, Qnew, 0500|DMDIR },
80 { ".", QTDIR, Qdir, 0500|DMDIR },
81 { "addr", QTFILE, QWaddr, 0600 },
82 { "body", QTAPPEND, QWbody, 0600|DMAPPEND },
83 { "ctl", QTFILE, QWctl, 0600 },
84 { "data", QTFILE, QWdata, 0600 },
85 { "editout", QTFILE, QWeditout, 0200 },
86 { "event", QTFILE, QWevent, 0600 },
87 { "rdsel", QTFILE, QWrdsel, 0400 },
88 { "wrsel", QTFILE, QWwrsel, 0200 },
89 { "tag", QTAPPEND, QWtag, 0600|DMAPPEND },
93 typedef struct Mnt Mnt;
103 Xfid* respond(Xfid*, Fcall*, char*);
104 int dostat(int, Dirtab*, uchar*, int, uint);
107 char *user = "Wile E. Coyote";
108 static int closing = 0;
109 int messagesize = Maxblock+IOHDRSZ; /* good start */
111 void fsysproc(void *);
121 error("can't create pipe");
122 if(post9pservice(p[0], "acme") < 0)
123 error("can't post service");
125 fmtinstall('F', fcallfmt);
126 if((u = getuser()) != nil)
128 threadcreate(fsysproc, nil, STACK);
143 buf = emalloc(messagesize+UTFmax); /* overflow for appending partial rune in xfidwrite */
144 n = threadread9pmsg(sfd, buf, messagesize);
148 error("i/o error on server channel");
151 sendp(cxfidalloc, nil);
152 x = recvp(cxfidalloc);
155 if(convM2S(buf, n, &x->fcall) != n)
156 error("convert error in convM2S");
158 fprint(2, "%F\n", &x->fcall);
159 if(fcall[x->fcall.type] == 0)
160 x = respond(x, &t, "bad fcall type");
162 if(x->fcall.type==Tversion || x->fcall.type==Tauth)
165 f = newfid(x->fcall.fid);
167 x = (*fcall[x->fcall.type])(x, f);
173 fsysaddid(Rune *dir, int ndir, Rune **incl, int nincl)
180 m = emalloc(sizeof *m);
183 m->ref = 1; /* one for Command, one will be incremented in attach */
202 fsysdelid(Mntdir *idm)
216 for(m=mnt.md; m; m=m->next){
219 prev->next = m->next;
222 for(i=0; i<m->nincl; i++)
233 sprint(buf, "fsysdelid: can't find id %d\n", idm->id);
234 sendp(cerr, estrdup(buf));
238 * Called only in exec.c:/^run(), from a different FD group
241 fsysmount(Rune *dir, int ndir, Rune **incl, int nincl)
243 return fsysaddid(dir, ndir, incl, nincl);
254 respond(Xfid *x, Fcall *t, char *err)
262 t->type = x->fcall.type+1;
263 t->fid = x->fcall.fid;
264 t->tag = x->fcall.tag;
266 x->buf = emalloc(messagesize);
267 n = convS2M(t, x->buf, messagesize);
270 fprint(2, "convert error (n=%d, msgsize %d): have %F\n", n, messagesize, &x->fcall);
271 fprint(2, "\tresponse: %F\n", t);
272 error("convert error in convS2M");
274 if(write(sfd, x->buf, n) != n)
275 error("write error in respond");
279 fprint(2, "r: %F\n", t);
285 fsysversion(Xfid *x, Fid *f)
290 if(x->fcall.msize < 256)
291 return respond(x, &t, "version: message size too small");
292 messagesize = x->fcall.msize;
293 t.msize = messagesize;
294 if(strncmp(x->fcall.version, "9P2000", 6) != 0)
295 return respond(x, &t, "unrecognized 9P version");
296 t.version = "9P2000";
297 return respond(x, &t, nil);
302 fsysauth(Xfid *x, Fid *f)
305 return respond(x, nil, "acme: authentication not required");
310 fsysflush(Xfid *x, Fid *f)
313 sendp(x->c, (void*)xfidflush);
319 fsysattach(Xfid *x, Fid *f)
326 if(strcmp(x->fcall.uname, user) != 0)
327 return respond(x, &t, Eperm);
338 id = atoi(x->fcall.aname);
340 for(m=mnt.md; m; m=m->next)
346 if(m == nil && x->fcall.aname[0]){
347 snprint(buf, sizeof buf, "unknown id '%s' in attach", x->fcall.aname);
348 sendp(cerr, estrdup(buf));
351 return respond(x, &t, nil);
356 fsyswalk(Xfid *x, Fid *f)
371 return respond(x, &t, "walk of open file");
372 if(x->fcall.fid != x->fcall.newfid){
373 nf = newfid(x->fcall.newfid);
375 return respond(x, &t, "newfid already in use");
378 nf->mntdir = f->mntdir;
384 nf->nrpart = 0; /* not open, so must be zero */
396 if(x->fcall.nwname > 0){
397 for(i=0; i<x->fcall.nwname; i++){
398 if((q.type & QTDIR) == 0){
403 if(strcmp(x->fcall.wname[i], "..") == 0){
413 err = "name too long";
418 q.path = QID(id, path);
419 t.wqid[t.nwqid++] = q;
423 /* is it a numeric name? */
424 for(j=0; (c=x->fcall.wname[i][j]); j++)
427 /* yes: it's a directory */
428 if(w) /* name has form 27/23; get out before losing w */
430 id = atoi(x->fcall.wname[i]);
432 w = lookid(id, FALSE);
437 incref(&w->ref); /* we'll drop reference at end if there's an error */
445 // if(FILE(f->qid) == Qacme) /* empty directory */
447 if(strcmp(x->fcall.wname[i], "new") == 0){
449 error("w set in walk to new");
450 sendp(cnewwindow, nil); /* signal newwindowthread */
451 w = recvp(cnewwindow); /* receive new window */
454 path = QID(w->id, Qdir);
466 if(strcmp(x->fcall.wname[i], d->name) == 0){
473 break; /* file not found */
476 if(i==0 && err == nil)
480 if(err!=nil || t.nwqid<x->fcall.nwname){
483 fsysdelid(nf->mntdir);
485 }else if(t.nwqid == x->fcall.nwname){
488 w = nil; /* don't drop the reference */
498 return respond(x, &t, err);
503 fsysopen(Xfid *x, Fid *f)
508 /* can't truncate anything, so just disregard */
509 x->fcall.mode &= ~(OTRUNC|OCEXEC);
510 /* can't execute or remove anything */
511 if(x->fcall.mode==OEXEC || (x->fcall.mode&ORCLOSE))
513 switch(x->fcall.mode){
526 if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
529 sendp(x->c, (void*)xfidopen);
533 return respond(x, &t, Eperm);
538 fsyscreate(Xfid *x, Fid *f)
543 return respond(x, &t, Eperm);
548 idcmp(const void *a, const void *b)
550 return *(int*)a - *(int*)b;
555 fsysread(Xfid *x, Fid *f)
559 int i, id, n, o, e, j, k, *ids, nids;
565 if(f->qid.type & QTDIR){
566 if(FILE(f->qid) == Qacme){ /* empty dir */
573 e = x->fcall.offset+x->fcall.count;
575 b = emalloc(messagesize);
582 d++; /* first entry is '.' */
583 for(i=0; d->name!=nil && i<e; i+=len){
584 len = dostat(WIN(x->f->qid), d, b+n, x->fcall.count-n, clock);
595 for(j=0; j<row.ncol; j++){
597 for(k=0; k<c->nw; k++){
598 ids = realloc(ids, (nids+1)*sizeof(int));
599 ids[nids++] = c->w[k]->id;
603 qsort(ids, nids, sizeof ids[0], idcmp);
606 for(; j<nids && i<e; i+=len){
608 sprint(dt.name, "%d", k);
609 dt.qid = QID(k, Qdir);
611 dt.perm = DMDIR|0700;
612 len = dostat(k, &dt, b+n, x->fcall.count-n, clock);
627 sendp(x->c, (void*)xfidread);
633 fsyswrite(Xfid *x, Fid *f)
636 sendp(x->c, (void*)xfidwrite);
642 fsysclunk(Xfid *x, Fid *f)
644 fsysdelid(f->mntdir);
645 sendp(x->c, (void*)xfidclose);
651 fsysremove(Xfid *x, Fid *f)
656 return respond(x, &t, Eperm);
661 fsysstat(Xfid *x, Fid *f)
665 t.stat = emalloc(messagesize-IOHDRSZ);
666 t.nstat = dostat(WIN(x->f->qid), f->dir, t.stat, messagesize-IOHDRSZ, getclock());
667 x = respond(x, &t, nil);
674 fsyswstat(Xfid *x, Fid *f)
679 return respond(x, &t, Eperm);
688 fh = &fids[fid&(Nhash-1)];
689 for(f=*fh; f; f=f->next)
692 else if(ff==nil && f->busy==FALSE)
698 f = emalloc(sizeof *f);
712 pread(clockfd, buf, sizeof buf, 0);
719 dostat(int id, Dirtab *dir, uchar *buf, int nbuf, uint clock)
723 d.qid.path = QID(id, dir->qid);
725 d.qid.type = dir->type;
727 d.length = 0; /* would be nice to do better */
734 return convD2M(&d, buf, nbuf);