23 static Fid *fids[Nhash];
27 static Xfid* fsysflush(Xfid*, Fid*);
28 static Xfid* fsysauth(Xfid*, Fid*);
29 static Xfid* fsysversion(Xfid*, Fid*);
30 static Xfid* fsysattach(Xfid*, Fid*);
31 static Xfid* fsyswalk(Xfid*, Fid*);
32 static Xfid* fsysopen(Xfid*, Fid*);
33 static Xfid* fsyscreate(Xfid*, Fid*);
34 static Xfid* fsysread(Xfid*, Fid*);
35 static Xfid* fsyswrite(Xfid*, Fid*);
36 static Xfid* fsysclunk(Xfid*, Fid*);
37 static Xfid* fsysremove(Xfid*, Fid*);
38 static Xfid* fsysstat(Xfid*, Fid*);
39 static Xfid* fsyswstat(Xfid*, Fid*);
41 Xfid* (*fcall[Tmax])(Xfid*, Fid*);
46 fcall[Tflush] = fsysflush;
47 fcall[Tversion] = fsysversion;
48 fcall[Tauth] = fsysauth;
49 fcall[Tattach] = fsysattach;
50 fcall[Twalk] = fsyswalk;
51 fcall[Topen] = fsysopen;
52 fcall[Tcreate] = fsyscreate;
53 fcall[Tread] = fsysread;
54 fcall[Twrite] = fsyswrite;
55 fcall[Tclunk] = fsysclunk;
56 fcall[Tremove]= fsysremove;
57 fcall[Tstat] = fsysstat;
58 fcall[Twstat] = fsyswstat;
61 char Eperm[] = "permission denied";
62 char Eexist[] = "file does not exist";
63 char Enotdir[] = "not a directory";
67 { ".", QTDIR, Qdir, 0500|DMDIR },
68 { "acme", QTDIR, Qacme, 0500|DMDIR },
69 { "cons", QTFILE, Qcons, 0600 },
70 { "consctl", QTFILE, Qconsctl, 0000 },
71 { "draw", QTDIR, Qdraw, 0000|DMDIR }, /* to suppress graphics progs started in acme */
72 { "editout", QTFILE, Qeditout, 0200 },
73 { "index", QTFILE, Qindex, 0400 },
74 { "label", QTFILE, Qlabel, 0600 },
75 { "log", QTFILE, Qlog, 0400 },
76 { "new", QTDIR, Qnew, 0500|DMDIR },
82 { ".", QTDIR, Qdir, 0500|DMDIR },
83 { "addr", QTFILE, QWaddr, 0600 },
84 { "body", QTAPPEND, QWbody, 0600|DMAPPEND },
85 { "ctl", QTFILE, QWctl, 0600 },
86 { "data", QTFILE, QWdata, 0600 },
87 { "editout", QTFILE, QWeditout, 0200 },
88 { "errors", QTFILE, QWerrors, 0200 },
89 { "event", QTFILE, QWevent, 0600 },
90 { "rdsel", QTFILE, QWrdsel, 0400 },
91 { "wrsel", QTFILE, QWwrsel, 0200 },
92 { "tag", QTAPPEND, QWtag, 0600|DMAPPEND },
93 { "xdata", QTFILE, QWxdata, 0600 },
97 typedef struct Mnt Mnt;
107 Xfid* respond(Xfid*, Fcall*, char*);
108 int dostat(int, Dirtab*, uchar*, int, uint);
111 char *user = "Wile E. Coyote";
112 static int closing = 0;
113 int messagesize = Maxblock+IOHDRSZ; /* good start */
115 void fsysproc(void *);
125 error("can't create pipe");
126 if(post9pservice(p[0], "acme", mtpt) < 0)
127 error("can't post service");
129 fmtinstall('F', fcallfmt);
130 if((u = getuser()) != nil)
132 proccreate(fsysproc, nil, STACK);
144 threadsetname("fsysproc");
149 buf = emalloc(messagesize+UTFmax); /* overflow for appending partial rune in xfidwrite */
150 n = read9pmsg(sfd, buf, messagesize);
154 error("i/o error on server channel");
157 sendp(cxfidalloc, nil);
158 x = recvp(cxfidalloc);
161 if(convM2S(buf, n, &x->fcall) != n)
162 error("convert error in convM2S");
164 fprint(2, "%F\n", &x->fcall);
165 if(fcall[x->fcall.type] == nil)
166 x = respond(x, &t, "bad fcall type");
168 switch(x->fcall.type){
175 f = newfid(x->fcall.fid);
178 f = newfid(x->fcall.fid);
181 x = respond(x, &t, "fid not in use");
187 x = (*fcall[x->fcall.type])(x, f);
193 fsysaddid(Rune *dir, int ndir, Rune **incl, int nincl)
200 m = emalloc(sizeof *m);
203 m->ref = 1; /* one for Command, one will be incremented in attach */
222 fsysdelid(Mntdir *idm)
236 for(m=mnt.md; m; m=m->next){
239 prev->next = m->next;
242 for(i=0; i<m->nincl; i++)
253 sprint(buf, "fsysdelid: can't find id %d\n", idm->id);
254 sendp(cerr, estrdup(buf));
258 * Called only in exec.c:/^run(), from a different FD group
261 fsysmount(Rune *dir, int ndir, Rune **incl, int nincl)
263 return fsysaddid(dir, ndir, incl, nincl);
271 * apparently this is not kosher on openbsd.
272 * perhaps because fsysproc is reading from sfd right now,
273 * the close hangs indefinitely.
279 respond(Xfid *x, Fcall *t, char *err)
287 t->type = x->fcall.type+1;
288 t->fid = x->fcall.fid;
289 t->tag = x->fcall.tag;
291 x->buf = emalloc(messagesize);
292 n = convS2M(t, x->buf, messagesize);
294 error("convert error in convS2M");
295 if(write(sfd, x->buf, n) != n)
296 error("write error in respond");
300 fprint(2, "r: %F\n", t);
306 fsysversion(Xfid *x, Fid *f)
311 if(x->fcall.msize < 256)
312 return respond(x, &t, "version: message size too small");
313 messagesize = x->fcall.msize;
314 t.msize = messagesize;
315 if(strncmp(x->fcall.version, "9P2000", 6) != 0)
316 return respond(x, &t, "unrecognized 9P version");
317 t.version = "9P2000";
318 return respond(x, &t, nil);
323 fsysauth(Xfid *x, Fid *f)
328 return respond(x, &t, "acme: authentication not required");
333 fsysflush(Xfid *x, Fid *f)
336 sendp(x->c, (void*)xfidflush);
342 fsysattach(Xfid *x, Fid *f)
349 if(strcmp(x->fcall.uname, user) != 0)
350 return respond(x, &t, Eperm);
361 id = atoi(x->fcall.aname);
363 for(m=mnt.md; m; m=m->next)
369 if(m == nil && x->fcall.aname[0]){
370 snprint(buf, sizeof buf, "unknown id '%s' in attach", x->fcall.aname);
371 sendp(cerr, estrdup(buf));
374 return respond(x, &t, nil);
379 fsyswalk(Xfid *x, Fid *f)
394 return respond(x, &t, "walk of open file");
395 if(x->fcall.fid != x->fcall.newfid){
396 nf = newfid(x->fcall.newfid);
398 return respond(x, &t, "newfid already in use");
401 nf->mntdir = f->mntdir;
407 nf->nrpart = 0; /* not open, so must be zero */
419 if(x->fcall.nwname > 0){
420 for(i=0; i<x->fcall.nwname; i++){
421 if((q.type & QTDIR) == 0){
426 if(strcmp(x->fcall.wname[i], "..") == 0){
436 err = "name too long";
441 q.path = QID(id, path);
442 t.wqid[t.nwqid++] = q;
446 /* is it a numeric name? */
447 for(j=0; (c=x->fcall.wname[i][j]); j++)
450 /* yes: it's a directory */
451 if(w) /* name has form 27/23; get out before losing w */
453 id = atoi(x->fcall.wname[i]);
455 w = lookid(id, FALSE);
460 incref(&w->ref); /* we'll drop reference at end if there's an error */
468 if(strcmp(x->fcall.wname[i], "new") == 0){
470 error("w set in walk to new");
471 sendp(cnewwindow, nil); /* signal newwindowthread */
472 w = recvp(cnewwindow); /* receive new window */
475 path = QID(w->id, Qdir);
487 if(strcmp(x->fcall.wname[i], d->name) == 0){
494 break; /* file not found */
497 if(i==0 && err == nil)
501 if(err!=nil || t.nwqid<x->fcall.nwname){
504 fsysdelid(nf->mntdir);
506 }else if(t.nwqid == x->fcall.nwname){
509 w = nil; /* don't drop the reference */
519 return respond(x, &t, err);
524 fsysopen(Xfid *x, Fid *f)
529 /* can't truncate anything, so just disregard */
530 x->fcall.mode &= ~(OTRUNC|OCEXEC);
531 /* can't execute or remove anything */
532 if(x->fcall.mode==OEXEC || (x->fcall.mode&ORCLOSE))
534 switch(x->fcall.mode){
547 if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
550 sendp(x->c, (void*)xfidopen);
554 return respond(x, &t, Eperm);
559 fsyscreate(Xfid *x, Fid *f)
564 return respond(x, &t, Eperm);
569 idcmp(const void *a, const void *b)
571 return *(int*)a - *(int*)b;
576 fsysread(Xfid *x, Fid *f)
580 int i, id, n, o, e, j, k, *ids, nids;
586 if(f->qid.type & QTDIR){
587 if(FILE(f->qid) == Qacme){ /* empty dir */
594 e = x->fcall.offset+x->fcall.count;
596 b = emalloc(messagesize);
603 d++; /* first entry is '.' */
604 for(i=0; d->name!=nil && i<e; i+=len){
605 len = dostat(WIN(x->f->qid), d, b+n, x->fcall.count-n, clock);
616 for(j=0; j<row.ncol; j++){
618 for(k=0; k<c->nw; k++){
619 ids = realloc(ids, (nids+1)*sizeof(int));
620 ids[nids++] = c->w[k]->id;
624 qsort(ids, nids, sizeof ids[0], idcmp);
627 for(; j<nids && i<e; i+=len){
629 sprint(dt.name, "%d", k);
630 dt.qid = QID(k, Qdir);
632 dt.perm = DMDIR|0700;
633 len = dostat(k, &dt, b+n, x->fcall.count-n, clock);
648 sendp(x->c, (void*)xfidread);
654 fsyswrite(Xfid *x, Fid *f)
657 sendp(x->c, (void*)xfidwrite);
663 fsysclunk(Xfid *x, Fid *f)
665 fsysdelid(f->mntdir);
666 sendp(x->c, (void*)xfidclose);
672 fsysremove(Xfid *x, Fid *f)
677 return respond(x, &t, Eperm);
682 fsysstat(Xfid *x, Fid *f)
686 t.stat = emalloc(messagesize-IOHDRSZ);
687 t.nstat = dostat(WIN(x->f->qid), f->dir, t.stat, messagesize-IOHDRSZ, getclock());
688 x = respond(x, &t, nil);
695 fsyswstat(Xfid *x, Fid *f)
700 return respond(x, &t, Eperm);
709 fh = &fids[fid&(Nhash-1)];
710 for(f=*fh; f; f=f->next)
713 else if(ff==nil && f->busy==FALSE)
719 f = emalloc(sizeof *f);
733 dostat(int id, Dirtab *dir, uchar *buf, int nbuf, uint clock)
737 d.qid.path = QID(id, dir->qid);
739 d.qid.type = dir->type;
741 d.length = 0; /* would be nice to do better */
748 return convD2M(&d, buf, nbuf);