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 },
90 { "xdata", QTFILE, QWxdata, 0600 },
94 typedef struct Mnt Mnt;
104 Xfid* respond(Xfid*, Fcall*, char*);
105 int dostat(int, Dirtab*, uchar*, int, uint);
108 char *user = "Wile E. Coyote";
109 static int closing = 0;
110 int messagesize = Maxblock+IOHDRSZ; /* good start */
112 void fsysproc(void *);
122 error("can't create pipe");
123 if(post9pservice(p[0], "acme") < 0)
124 error("can't post service");
126 fmtinstall('F', fcallfmt);
127 if((u = getuser()) != nil)
129 threadcreate(fsysproc, nil, STACK);
144 buf = emalloc(messagesize+UTFmax); /* overflow for appending partial rune in xfidwrite */
145 n = threadread9pmsg(sfd, buf, messagesize);
149 error("i/o error on server channel");
152 sendp(cxfidalloc, nil);
153 x = recvp(cxfidalloc);
156 if(convM2S(buf, n, &x->fcall) != n)
157 error("convert error in convM2S");
159 fprint(2, "%F\n", &x->fcall);
160 if(fcall[x->fcall.type] == 0)
161 x = respond(x, &t, "bad fcall type");
163 if(x->fcall.type==Tversion || x->fcall.type==Tauth)
166 f = newfid(x->fcall.fid);
168 x = (*fcall[x->fcall.type])(x, f);
174 fsysaddid(Rune *dir, int ndir, Rune **incl, int nincl)
181 m = emalloc(sizeof *m);
184 m->ref = 1; /* one for Command, one will be incremented in attach */
203 fsysdelid(Mntdir *idm)
217 for(m=mnt.md; m; m=m->next){
220 prev->next = m->next;
223 for(i=0; i<m->nincl; i++)
234 sprint(buf, "fsysdelid: can't find id %d\n", idm->id);
235 sendp(cerr, estrdup(buf));
239 * Called only in exec.c:/^run(), from a different FD group
242 fsysmount(Rune *dir, int ndir, Rune **incl, int nincl)
244 return fsysaddid(dir, ndir, incl, nincl);
255 respond(Xfid *x, Fcall *t, char *err)
263 t->type = x->fcall.type+1;
264 t->fid = x->fcall.fid;
265 t->tag = x->fcall.tag;
267 x->buf = emalloc(messagesize);
268 n = convS2M(t, x->buf, messagesize);
271 fprint(2, "convert error (n=%d, msgsize %d): have %F\n", n, messagesize, &x->fcall);
272 fprint(2, "\tresponse: %F\n", t);
273 error("convert error in convS2M");
275 if(write(sfd, x->buf, n) != n)
276 error("write error in respond");
280 fprint(2, "r: %F\n", t);
286 fsysversion(Xfid *x, Fid *f)
291 if(x->fcall.msize < 256)
292 return respond(x, &t, "version: message size too small");
293 messagesize = x->fcall.msize;
294 t.msize = messagesize;
295 if(strncmp(x->fcall.version, "9P2000", 6) != 0)
296 return respond(x, &t, "unrecognized 9P version");
297 t.version = "9P2000";
298 return respond(x, &t, nil);
303 fsysauth(Xfid *x, Fid *f)
306 return respond(x, nil, "acme: authentication not required");
311 fsysflush(Xfid *x, Fid *f)
314 sendp(x->c, (void*)xfidflush);
320 fsysattach(Xfid *x, Fid *f)
327 if(strcmp(x->fcall.uname, user) != 0)
328 return respond(x, &t, Eperm);
339 id = atoi(x->fcall.aname);
341 for(m=mnt.md; m; m=m->next)
347 if(m == nil && x->fcall.aname[0]){
348 snprint(buf, sizeof buf, "unknown id '%s' in attach", x->fcall.aname);
349 sendp(cerr, estrdup(buf));
352 return respond(x, &t, nil);
357 fsyswalk(Xfid *x, Fid *f)
372 return respond(x, &t, "walk of open file");
373 if(x->fcall.fid != x->fcall.newfid){
374 nf = newfid(x->fcall.newfid);
376 return respond(x, &t, "newfid already in use");
379 nf->mntdir = f->mntdir;
385 nf->nrpart = 0; /* not open, so must be zero */
397 if(x->fcall.nwname > 0){
398 for(i=0; i<x->fcall.nwname; i++){
399 if((q.type & QTDIR) == 0){
404 if(strcmp(x->fcall.wname[i], "..") == 0){
414 err = "name too long";
419 q.path = QID(id, path);
420 t.wqid[t.nwqid++] = q;
424 /* is it a numeric name? */
425 for(j=0; (c=x->fcall.wname[i][j]); j++)
428 /* yes: it's a directory */
429 if(w) /* name has form 27/23; get out before losing w */
431 id = atoi(x->fcall.wname[i]);
433 w = lookid(id, FALSE);
438 incref(&w->ref); /* we'll drop reference at end if there's an error */
446 // if(FILE(f->qid) == Qacme) /* empty directory */
448 if(strcmp(x->fcall.wname[i], "new") == 0){
450 error("w set in walk to new");
451 sendp(cnewwindow, nil); /* signal newwindowthread */
452 w = recvp(cnewwindow); /* receive new window */
455 path = QID(w->id, Qdir);
467 if(strcmp(x->fcall.wname[i], d->name) == 0){
474 break; /* file not found */
477 if(i==0 && err == nil)
481 if(err!=nil || t.nwqid<x->fcall.nwname){
484 fsysdelid(nf->mntdir);
486 }else if(t.nwqid == x->fcall.nwname){
489 w = nil; /* don't drop the reference */
499 return respond(x, &t, err);
504 fsysopen(Xfid *x, Fid *f)
509 /* can't truncate anything, so just disregard */
510 x->fcall.mode &= ~(OTRUNC|OCEXEC);
511 /* can't execute or remove anything */
512 if(x->fcall.mode==OEXEC || (x->fcall.mode&ORCLOSE))
514 switch(x->fcall.mode){
527 if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
530 sendp(x->c, (void*)xfidopen);
534 return respond(x, &t, Eperm);
539 fsyscreate(Xfid *x, Fid *f)
544 return respond(x, &t, Eperm);
549 idcmp(const void *a, const void *b)
551 return *(int*)a - *(int*)b;
556 fsysread(Xfid *x, Fid *f)
560 int i, id, n, o, e, j, k, *ids, nids;
566 if(f->qid.type & QTDIR){
567 if(FILE(f->qid) == Qacme){ /* empty dir */
574 e = x->fcall.offset+x->fcall.count;
576 b = emalloc(messagesize);
583 d++; /* first entry is '.' */
584 for(i=0; d->name!=nil && i<e; i+=len){
585 len = dostat(WIN(x->f->qid), d, b+n, x->fcall.count-n, clock);
596 for(j=0; j<row.ncol; j++){
598 for(k=0; k<c->nw; k++){
599 ids = realloc(ids, (nids+1)*sizeof(int));
600 ids[nids++] = c->w[k]->id;
604 qsort(ids, nids, sizeof ids[0], idcmp);
607 for(; j<nids && i<e; i+=len){
609 sprint(dt.name, "%d", k);
610 dt.qid = QID(k, Qdir);
612 dt.perm = DMDIR|0700;
613 len = dostat(k, &dt, b+n, x->fcall.count-n, clock);
628 sendp(x->c, (void*)xfidread);
634 fsyswrite(Xfid *x, Fid *f)
637 sendp(x->c, (void*)xfidwrite);
643 fsysclunk(Xfid *x, Fid *f)
645 fsysdelid(f->mntdir);
646 sendp(x->c, (void*)xfidclose);
652 fsysremove(Xfid *x, Fid *f)
657 return respond(x, &t, Eperm);
662 fsysstat(Xfid *x, Fid *f)
666 t.stat = emalloc(messagesize-IOHDRSZ);
667 t.nstat = dostat(WIN(x->f->qid), f->dir, t.stat, messagesize-IOHDRSZ, getclock());
668 x = respond(x, &t, nil);
675 fsyswstat(Xfid *x, Fid *f)
680 return respond(x, &t, Eperm);
689 fh = &fids[fid&(Nhash-1)];
690 for(f=*fh; f; f=f->next)
693 else if(ff==nil && f->busy==FALSE)
699 f = emalloc(sizeof *f);
713 pread(clockfd, buf, sizeof buf, 0);
720 dostat(int id, Dirtab *dir, uchar *buf, int nbuf, uint clock)
724 d.qid.path = QID(id, dir->qid);
726 d.qid.type = dir->type;
728 d.length = 0; /* would be nice to do better */
735 return convD2M(&d, buf, nbuf);