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") < 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);
293 fprint(2, "convert error (n=%d, msgsize %d): have %F\n", n, messagesize, &x->fcall);
294 fprint(2, "\tresponse: %F\n", t);
295 error("convert error in convS2M");
297 if(write(sfd, x->buf, n) != n)
298 error("write error in respond");
302 fprint(2, "r: %F\n", t);
308 fsysversion(Xfid *x, Fid *f)
313 if(x->fcall.msize < 256)
314 return respond(x, &t, "version: message size too small");
315 messagesize = x->fcall.msize;
316 t.msize = messagesize;
317 if(strncmp(x->fcall.version, "9P2000", 6) != 0)
318 return respond(x, &t, "unrecognized 9P version");
319 t.version = "9P2000";
320 return respond(x, &t, nil);
325 fsysauth(Xfid *x, Fid *f)
330 return respond(x, &t, "acme: authentication not required");
335 fsysflush(Xfid *x, Fid *f)
338 sendp(x->c, (void*)xfidflush);
344 fsysattach(Xfid *x, Fid *f)
351 if(strcmp(x->fcall.uname, user) != 0)
352 return respond(x, &t, Eperm);
363 id = atoi(x->fcall.aname);
365 for(m=mnt.md; m; m=m->next)
371 if(m == nil && x->fcall.aname[0]){
372 snprint(buf, sizeof buf, "unknown id '%s' in attach", x->fcall.aname);
373 sendp(cerr, estrdup(buf));
376 return respond(x, &t, nil);
381 fsyswalk(Xfid *x, Fid *f)
396 return respond(x, &t, "walk of open file");
397 if(x->fcall.fid != x->fcall.newfid){
398 nf = newfid(x->fcall.newfid);
400 return respond(x, &t, "newfid already in use");
403 nf->mntdir = f->mntdir;
409 nf->nrpart = 0; /* not open, so must be zero */
421 if(x->fcall.nwname > 0){
422 for(i=0; i<x->fcall.nwname; i++){
423 if((q.type & QTDIR) == 0){
428 if(strcmp(x->fcall.wname[i], "..") == 0){
438 err = "name too long";
443 q.path = QID(id, path);
444 t.wqid[t.nwqid++] = q;
448 /* is it a numeric name? */
449 for(j=0; (c=x->fcall.wname[i][j]); j++)
452 /* yes: it's a directory */
453 if(w) /* name has form 27/23; get out before losing w */
455 id = atoi(x->fcall.wname[i]);
457 w = lookid(id, FALSE);
462 incref(&w->ref); /* we'll drop reference at end if there's an error */
471 if(FILE(f->qid) == Qacme) // empty directory
474 if(strcmp(x->fcall.wname[i], "new") == 0){
476 error("w set in walk to new");
477 sendp(cnewwindow, nil); /* signal newwindowthread */
478 w = recvp(cnewwindow); /* receive new window */
481 path = QID(w->id, Qdir);
493 if(strcmp(x->fcall.wname[i], d->name) == 0){
500 break; /* file not found */
503 if(i==0 && err == nil)
507 if(err!=nil || t.nwqid<x->fcall.nwname){
510 fsysdelid(nf->mntdir);
512 }else if(t.nwqid == x->fcall.nwname){
515 w = nil; /* don't drop the reference */
525 return respond(x, &t, err);
530 fsysopen(Xfid *x, Fid *f)
535 /* can't truncate anything, so just disregard */
536 x->fcall.mode &= ~(OTRUNC|OCEXEC);
537 /* can't execute or remove anything */
538 if(x->fcall.mode==OEXEC || (x->fcall.mode&ORCLOSE))
540 switch(x->fcall.mode){
553 if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
556 sendp(x->c, (void*)xfidopen);
560 return respond(x, &t, Eperm);
565 fsyscreate(Xfid *x, Fid *f)
570 return respond(x, &t, Eperm);
575 idcmp(const void *a, const void *b)
577 return *(int*)a - *(int*)b;
582 fsysread(Xfid *x, Fid *f)
586 int i, id, n, o, e, j, k, *ids, nids;
592 if(f->qid.type & QTDIR){
593 if(FILE(f->qid) == Qacme){ /* empty dir */
600 e = x->fcall.offset+x->fcall.count;
602 b = emalloc(messagesize);
609 d++; /* first entry is '.' */
610 for(i=0; d->name!=nil && i<e; i+=len){
611 len = dostat(WIN(x->f->qid), d, b+n, x->fcall.count-n, clock);
622 for(j=0; j<row.ncol; j++){
624 for(k=0; k<c->nw; k++){
625 ids = realloc(ids, (nids+1)*sizeof(int));
626 ids[nids++] = c->w[k]->id;
630 qsort(ids, nids, sizeof ids[0], idcmp);
633 for(; j<nids && i<e; i+=len){
635 sprint(dt.name, "%d", k);
636 dt.qid = QID(k, Qdir);
638 dt.perm = DMDIR|0700;
639 len = dostat(k, &dt, b+n, x->fcall.count-n, clock);
654 sendp(x->c, (void*)xfidread);
660 fsyswrite(Xfid *x, Fid *f)
663 sendp(x->c, (void*)xfidwrite);
669 fsysclunk(Xfid *x, Fid *f)
671 fsysdelid(f->mntdir);
672 sendp(x->c, (void*)xfidclose);
678 fsysremove(Xfid *x, Fid *f)
683 return respond(x, &t, Eperm);
688 fsysstat(Xfid *x, Fid *f)
692 t.stat = emalloc(messagesize-IOHDRSZ);
693 t.nstat = dostat(WIN(x->f->qid), f->dir, t.stat, messagesize-IOHDRSZ, getclock());
694 x = respond(x, &t, nil);
701 fsyswstat(Xfid *x, Fid *f)
706 return respond(x, &t, Eperm);
715 fh = &fids[fid&(Nhash-1)];
716 for(f=*fh; f; f=f->next)
719 else if(ff==nil && f->busy==FALSE)
725 f = emalloc(sizeof *f);
739 pread(clockfd, buf, sizeof buf, 0);
746 dostat(int id, Dirtab *dir, uchar *buf, int nbuf, uint clock)
750 d.qid.path = QID(id, dir->qid);
752 d.qid.type = dir->type;
754 d.length = 0; /* would be nice to do better */
761 return convD2M(&d, buf, nbuf);