#include <bio.h>
#include <ndb.h>
#include <thread.h>
+#include <9p.h>
/*
* This fs presents a 1 level file system. It contains
*/
typedef struct Console Console;
-typedef struct Fid Fid;
+typedef struct Aux Aux;
typedef struct Request Request;
typedef struct Reqlist Reqlist;
typedef struct Fs Fs;
Bufsize= 32*1024, /* chars buffered per reader */
Maxcons= 64, /* maximum consoles */
- Nhash= 64, /* Fid hash buckets */
+ Nhash= 64, /* Aux hash buckets */
};
#define TYPE(x) (((ulong)x.path) & 0xf)
struct Request
{
Request *next;
- Fid *fid;
+ Aux *fid;
Fs *fs;
Fcall f;
uchar buf[1];
Request *last;
};
-struct Fid
+struct Aux
{
Lock;
- Fid *next; /* hash list */
- Fid *cnext; /* list of Fid's on a console */
+ Aux *next; /* hash list */
+ Aux *cnext; /* list of Aux's on a console */
int fid;
int ref;
int cfd;
int sfd;
- Fid *flist; /* open fids to broadcast to */
+ Aux *flist; /* open fids to broadcast to */
};
struct Fs
int fd; /* to kernel mount point */
int messagesize;
- Fid *hash[Nhash];
+ Aux *hash[Nhash];
Console *cons[Maxcons];
int ncons;
};
extern void fsreader(void*);
extern void fsrun(void*);
-extern Fid* fsgetfid(Fs*, int);
-extern void fsputfid(Fs*, Fid*);
+extern Aux* fsgetfid(Fs*, int);
+extern void fsputfid(Fs*, Aux*);
extern int fsdirgen(Fs*, Qid, int, Dir*, uchar*, int);
extern void fsreply(Fs*, Request*, char*);
-extern void fskick(Fs*, Fid*);
+extern void fskick(Fs*, Aux*);
extern int fsreopen(Fs*, Console*);
-extern void fsversion(Fs*, Request*, Fid*);
-extern void fsflush(Fs*, Request*, Fid*);
-extern void fsauth(Fs*, Request*, Fid*);
-extern void fsattach(Fs*, Request*, Fid*);
-extern void fswalk(Fs*, Request*, Fid*);
-extern void fsclwalk(Fs*, Request*, Fid*);
-extern void fsopen(Fs*, Request*, Fid*);
-extern void fscreate(Fs*, Request*, Fid*);
-extern void fsread(Fs*, Request*, Fid*);
-extern void fswrite(Fs*, Request*, Fid*);
-extern void fsclunk(Fs*, Request*, Fid*);
-extern void fsremove(Fs*, Request*, Fid*);
-extern void fsstat(Fs*, Request*, Fid*);
-extern void fswstat(Fs*, Request*, Fid*);
-
-
-void (*fcall[])(Fs*, Request*, Fid*) =
+extern void fsversion(Fs*, Request*, Aux*);
+extern void fsflush(Fs*, Request*, Aux*);
+extern void fsauth(Fs*, Request*, Aux*);
+extern void fsattach(Fs*, Request*, Aux*);
+extern void fswalk(Fs*, Request*, Aux*);
+extern void fsclwalk(Fs*, Request*, Aux*);
+extern void fsopen(Fs*, Request*, Aux*);
+extern void fscreate(Fs*, Request*, Aux*);
+extern void fsread(Fs*, Request*, Aux*);
+extern void fswrite(Fs*, Request*, Aux*);
+extern void fsclunk(Fs*, Request*, Aux*);
+extern void fsremove(Fs*, Request*, Aux*);
+extern void fsstat(Fs*, Request*, Aux*);
+extern void fswstat(Fs*, Request*, Aux*);
+
+
+void (*fcall[])(Fs*, Request*, Aux*) =
{
[Tflush] fsflush,
[Tversion] fsversion,
* any request that can get queued for a delayed reply
*/
Request*
-allocreq(Fs *fs, int bufsize)
+alloccreq(Fs *fs, int bufsize)
{
Request *r;
Fs*
fsmount(char *mntpt)
{
+ int pfd[2];
+
Fs *fs;
- int pfd[2], srv;
- char buf[32];
- int n;
+ Dir d;
static void *v[2];
fs = emalloc(sizeof(Fs));
v[1] = pfd;
proccreate(fsrun, v, 16*1024);
- /* Typically mounted before /srv exists */
- if(access("#s/consoles", AEXIST) < 0){
- srv = create("#s/consoles", OWRITE, 0666);
- if(srv < 0)
- fatal("post: %r");
+ if(postfd("consoles", pfd[1]) < 0)
+ sysfatal("post: %r");
- n = sprint(buf, "%d", pfd[1]);
- if(write(srv, buf, n) < 0)
- fatal("write srv: %r");
+ nulldir(&d);
+ d.mode = 0666;
+ dirwstat("/srv/consoles", &d);
- close(srv);
- }
+ if(mntpt){
+ if(amount(pfd[1], mntpt, MBEFORE, "") == -1)
+ sysfatal("mount %s: %r", mntpt);
+ }else
+ close(pfd[1]);
- mount(pfd[1], -1, mntpt, MBEFORE, "");
- close(pfd[1]);
return fs;
}
* the reader may miss data but always sees an in order sequence.
*/
void
-fromconsole(Fid *f, char *p, int n)
+fromconsole(Aux *f, char *p, int n)
{
char *rp, *wp, *ep;
int pass;
* broadcast a list of members to all listeners
*/
void
-bcastmembers(Fs *fs, Console *c, char *msg, Fid *f)
+bcastmembers(Fs *fs, Console *c, char *msg, Aux *f)
{
int n;
- Fid *fl;
+ Aux *fl;
char buf[512];
sprint(buf, "[%s%s", msg, f->user);
fsreader(void *v)
{
int n;
- Fid *fl;
+ Aux *fl;
char buf[1024];
Fs *fs;
Console *c;
}
}
-int dbmtime;
-
/*
* a request processor (one per Fs)
*/
{
int n, t;
Request *r;
- Fid *f;
- Dir *d;
+ Aux *f;
void **a = v;
Fs* fs;
int *pfd;
fs = a[0];
pfd = a[1];
fs->fd = pfd[0];
+ readdb(fs);
notify(handler);
for(;;){
- d = dirstat(consoledb);
- if(d != nil && d->mtime != dbmtime){
- dbmtime = d->mtime;
+ if(ndbchanged(db))
readdb(fs);
- }
- free(d);
- r = allocreq(fs, messagesize);
- n = read9pmsg(fs->fd, r->buf, messagesize);
- if(n <= 0)
+ r = alloccreq(fs, messagesize);
+ while((n = read9pmsg(fs->fd, r->buf, messagesize)) == 0)
+ ;
+ if(n < 0)
fatal("unmounted");
if(convM2S(r->buf, n, &r->f) == 0){
}
}
-Fid*
+Aux*
fsgetfid(Fs *fs, int fid)
{
- Fid *f, *nf;
+ Aux *f, *nf;
lock(fs);
for(f = fs->hash[fid%Nhash]; f; f = f->next){
}
}
- nf = emalloc(sizeof(Fid));
+ nf = emalloc(sizeof(Aux));
nf->next = fs->hash[fid%Nhash];
fs->hash[fid%Nhash] = nf;
nf->fid = fid;
}
void
-fsputfid(Fs *fs, Fid *f)
+fsputfid(Fs *fs, Aux *f)
{
- Fid **l, *nf;
+ Aux **l, *nf;
lock(fs);
if(--f->ref > 0){
}
void
-fsauth(Fs *fs, Request *r, Fid*)
+fsauth(Fs *fs, Request *r, Aux*)
{
fsreply(fs, r, "consolefs: authentication not required");
}
void
-fsversion(Fs *fs, Request *r, Fid*)
+fsversion(Fs *fs, Request *r, Aux*)
{
if(r->f.msize < 256){
}
void
-fsflush(Fs *fs, Request *r, Fid *f)
+fsflush(Fs *fs, Request *r, Aux *f)
{
Request *or;
if(or != nil){
fsputfid(fs, or->fid);
free(or);
+ fsreply(fs, r, nil);
+ } else {
+ fsputfid(fs, f);
+ free(r);
}
- fsreply(fs, r, nil);
}
void
-fsattach(Fs *fs, Request *r, Fid *f)
+fsattach(Fs *fs, Request *r, Aux *f)
{
f->qid.type = QTDIR;
f->qid.path = QID(0, Ttopdir);
}
void
-fswalk(Fs *fs, Request *r, Fid *f)
+fswalk(Fs *fs, Request *r, Aux *f)
{
char *name;
Dir d;
int i, n, nqid, nwname;
Qid qid, wqid[MAXWELEM];
- Fid *nf;
+ Aux *nf;
char *err;
if(f->attached == 0){
void
bcastmsg(Fs *fs, Console *c, char *msg, int n)
{
- Fid *fl;
+ Aux *fl;
for(fl = c->flist; fl; fl = fl->cnext){
fromconsole(fl, msg, n);
}
void
-fsopen(Fs *fs, Request *r, Fid *f)
+fsopen(Fs *fs, Request *r, Aux *f)
{
int mode;
Console *c;
}
void
-fscreate(Fs *fs, Request *r, Fid*)
+fscreate(Fs *fs, Request *r, Aux*)
{
fsreply(fs, r, Eperm);
}
void
-fsread(Fs *fs, Request *r, Fid *f)
+fsread(Fs *fs, Request *r, Aux *f)
{
uchar *p, *e;
int i, m, off;
}
void
-fswrite(Fs *fs, Request *r, Fid *f)
+fswrite(Fs *fs, Request *r, Aux *f)
{
int i, eol = 0;
}
void
-fsclunk(Fs *fs, Request *r, Fid *f)
+fsclunk(Fs *fs, Request *r, Aux *f)
{
- Fid **l, *fl;
+ Aux **l, *fl;
Request *nr;
if(f->open && TYPE(f->qid) == Qdata){
}
void
-fsremove(Fs *fs, Request *r, Fid*)
+fsremove(Fs *fs, Request *r, Aux*)
{
fsreply(fs, r, Eperm);
}
void
-fsstat(Fs *fs, Request *r, Fid *f)
+fsstat(Fs *fs, Request *r, Aux *f)
{
int i, n;
Qid q;
}
void
-fswstat(Fs *fs, Request *r, Fid*)
+fswstat(Fs *fs, Request *r, Aux*)
{
fsreply(fs, r, Eperm);
}
* called whenever input or a read request has been received
*/
void
-fskick(Fs *fs, Fid *f)
+fskick(Fs *fs, Aux *f)
{
Request *r;
char *p, *rp, *wp, *ep;