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 { "errors", QTFILE, QWerrors, 0200 },
87 { "event", QTFILE, QWevent, 0600 },
88 { "rdsel", QTFILE, QWrdsel, 0400 },
89 { "wrsel", QTFILE, QWwrsel, 0200 },
90 { "tag", QTAPPEND, QWtag, 0600|DMAPPEND },
91 { "xdata", QTFILE, QWxdata, 0600 },
95 typedef struct Mnt Mnt;
105 Xfid* respond(Xfid*, Fcall*, char*);
106 int dostat(int, Dirtab*, uchar*, int, uint);
109 char *user = "Wile E. Coyote";
110 static int closing = 0;
111 int messagesize = Maxblock+IOHDRSZ; /* good start */
113 void fsysproc(void *);
123 error("can't create pipe");
124 if(post9pservice(p[0], "acme", mtpt) < 0)
125 error("can't post service");
127 fmtinstall('F', fcallfmt);
128 if((u = getuser()) != nil)
130 proccreate(fsysproc, nil, STACK);
142 threadsetname("fsysproc");
147 buf = emalloc(messagesize+UTFmax); /* overflow for appending partial rune in xfidwrite */
148 n = read9pmsg(sfd, buf, messagesize);
152 error("i/o error on server channel");
155 sendp(cxfidalloc, nil);
156 x = recvp(cxfidalloc);
159 if(convM2S(buf, n, &x->fcall) != n)
160 error("convert error in convM2S");
162 fprint(2, "%F\n", &x->fcall);
163 if(fcall[x->fcall.type] == nil)
164 x = respond(x, &t, "bad fcall type");
166 switch(x->fcall.type){
173 f = newfid(x->fcall.fid);
176 f = newfid(x->fcall.fid);
179 x = respond(x, &t, "fid not in use");
185 x = (*fcall[x->fcall.type])(x, f);
191 fsysaddid(Rune *dir, int ndir, Rune **incl, int nincl)
198 m = emalloc(sizeof *m);
201 m->ref = 1; /* one for Command, one will be incremented in attach */
220 fsysdelid(Mntdir *idm)
234 for(m=mnt.md; m; m=m->next){
237 prev->next = m->next;
240 for(i=0; i<m->nincl; i++)
251 sprint(buf, "fsysdelid: can't find id %d\n", idm->id);
252 sendp(cerr, estrdup(buf));
256 * Called only in exec.c:/^run(), from a different FD group
259 fsysmount(Rune *dir, int ndir, Rune **incl, int nincl)
261 return fsysaddid(dir, ndir, incl, nincl);
269 * apparently this is not kosher on openbsd.
270 * perhaps because fsysproc is reading from sfd right now,
271 * the close hangs indefinitely.
277 respond(Xfid *x, Fcall *t, char *err)
285 t->type = x->fcall.type+1;
286 t->fid = x->fcall.fid;
287 t->tag = x->fcall.tag;
289 x->buf = emalloc(messagesize);
290 n = convS2M(t, x->buf, messagesize);
292 error("convert error in convS2M");
293 if(write(sfd, x->buf, n) != n)
294 error("write error in respond");
298 fprint(2, "r: %F\n", t);
304 fsysversion(Xfid *x, Fid *f)
309 if(x->fcall.msize < 256)
310 return respond(x, &t, "version: message size too small");
311 messagesize = x->fcall.msize;
312 t.msize = messagesize;
313 if(strncmp(x->fcall.version, "9P2000", 6) != 0)
314 return respond(x, &t, "unrecognized 9P version");
315 t.version = "9P2000";
316 return respond(x, &t, nil);
321 fsysauth(Xfid *x, Fid *f)
326 return respond(x, &t, "acme: authentication not required");
331 fsysflush(Xfid *x, Fid *f)
334 sendp(x->c, (void*)xfidflush);
340 fsysattach(Xfid *x, Fid *f)
347 if(strcmp(x->fcall.uname, user) != 0)
348 return respond(x, &t, Eperm);
359 id = atoi(x->fcall.aname);
361 for(m=mnt.md; m; m=m->next)
367 if(m == nil && x->fcall.aname[0]){
368 snprint(buf, sizeof buf, "unknown id '%s' in attach", x->fcall.aname);
369 sendp(cerr, estrdup(buf));
372 return respond(x, &t, nil);
377 fsyswalk(Xfid *x, Fid *f)
392 return respond(x, &t, "walk of open file");
393 if(x->fcall.fid != x->fcall.newfid){
394 nf = newfid(x->fcall.newfid);
396 return respond(x, &t, "newfid already in use");
399 nf->mntdir = f->mntdir;
405 nf->nrpart = 0; /* not open, so must be zero */
417 if(x->fcall.nwname > 0){
418 for(i=0; i<x->fcall.nwname; i++){
419 if((q.type & QTDIR) == 0){
424 if(strcmp(x->fcall.wname[i], "..") == 0){
434 err = "name too long";
439 q.path = QID(id, path);
440 t.wqid[t.nwqid++] = q;
444 /* is it a numeric name? */
445 for(j=0; (c=x->fcall.wname[i][j]); j++)
448 /* yes: it's a directory */
449 if(w) /* name has form 27/23; get out before losing w */
451 id = atoi(x->fcall.wname[i]);
453 w = lookid(id, FALSE);
458 incref(&w->ref); /* we'll drop reference at end if there's an error */
466 if(strcmp(x->fcall.wname[i], "new") == 0){
468 error("w set in walk to new");
469 sendp(cnewwindow, nil); /* signal newwindowthread */
470 w = recvp(cnewwindow); /* receive new window */
473 path = QID(w->id, Qdir);
485 if(strcmp(x->fcall.wname[i], d->name) == 0){
492 break; /* file not found */
495 if(i==0 && err == nil)
499 if(err!=nil || t.nwqid<x->fcall.nwname){
502 fsysdelid(nf->mntdir);
504 }else if(t.nwqid == x->fcall.nwname){
507 w = nil; /* don't drop the reference */
517 return respond(x, &t, err);
522 fsysopen(Xfid *x, Fid *f)
527 /* can't truncate anything, so just disregard */
528 x->fcall.mode &= ~(OTRUNC|OCEXEC);
529 /* can't execute or remove anything */
530 if(x->fcall.mode==OEXEC || (x->fcall.mode&ORCLOSE))
532 switch(x->fcall.mode){
545 if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
548 sendp(x->c, (void*)xfidopen);
552 return respond(x, &t, Eperm);
557 fsyscreate(Xfid *x, Fid *f)
562 return respond(x, &t, Eperm);
567 idcmp(const void *a, const void *b)
569 return *(int*)a - *(int*)b;
574 fsysread(Xfid *x, Fid *f)
578 int i, id, n, o, e, j, k, *ids, nids;
584 if(f->qid.type & QTDIR){
585 if(FILE(f->qid) == Qacme){ /* empty dir */
592 e = x->fcall.offset+x->fcall.count;
594 b = emalloc(messagesize);
601 d++; /* first entry is '.' */
602 for(i=0; d->name!=nil && i<e; i+=len){
603 len = dostat(WIN(x->f->qid), d, b+n, x->fcall.count-n, clock);
614 for(j=0; j<row.ncol; j++){
616 for(k=0; k<c->nw; k++){
617 ids = realloc(ids, (nids+1)*sizeof(int));
618 ids[nids++] = c->w[k]->id;
622 qsort(ids, nids, sizeof ids[0], idcmp);
625 for(; j<nids && i<e; i+=len){
627 sprint(dt.name, "%d", k);
628 dt.qid = QID(k, Qdir);
630 dt.perm = DMDIR|0700;
631 len = dostat(k, &dt, b+n, x->fcall.count-n, clock);
646 sendp(x->c, (void*)xfidread);
652 fsyswrite(Xfid *x, Fid *f)
655 sendp(x->c, (void*)xfidwrite);
661 fsysclunk(Xfid *x, Fid *f)
663 fsysdelid(f->mntdir);
664 sendp(x->c, (void*)xfidclose);
670 fsysremove(Xfid *x, Fid *f)
675 return respond(x, &t, Eperm);
680 fsysstat(Xfid *x, Fid *f)
684 t.stat = emalloc(messagesize-IOHDRSZ);
685 t.nstat = dostat(WIN(x->f->qid), f->dir, t.stat, messagesize-IOHDRSZ, getclock());
686 x = respond(x, &t, nil);
693 fsyswstat(Xfid *x, Fid *f)
698 return respond(x, &t, Eperm);
707 fh = &fids[fid&(Nhash-1)];
708 for(f=*fh; f; f=f->next)
711 else if(ff==nil && f->busy==FALSE)
717 f = emalloc(sizeof *f);
731 dostat(int id, Dirtab *dir, uchar *buf, int nbuf, uint clock)
735 d.qid.path = QID(id, dir->qid);
737 d.qid.type = dir->type;
739 d.length = 0; /* would be nice to do better */
746 return convD2M(&d, buf, nbuf);