8 * Rather than reading /adm/users, which is a lot of work for
9 * a toy program, we assume all groups have the form
11 * meaning that each user is the leader of his own group.
16 OPERM = 0x3, /* mask of all permission types in open mode */
18 Maxsize = 512*1024*1024,
22 typedef struct Fid Fid;
23 typedef struct Ram Ram;
40 long parent; /* index in Ram array */
63 ulong path; /* incremented for each new file */
69 uchar mdata[IOHDRSZ+Maxfdata];
70 uchar rdata[Maxfdata]; /* buffer for data in reply */
71 uchar statbuf[STATMAX];
74 int messagesize = sizeof mdata;
77 uint ramstat(Ram*, uchar*, uint);
79 void *erealloc(void*, ulong);
82 void ramfsusage(void);
83 int perm(Fid*, Ram*, int);
86 char *rflush(Fid*), *rversion(Fid*), *rauth(Fid*),
87 *rattach(Fid*), *rwalk(Fid*),
88 *ropen(Fid*), *rcreate(Fid*),
89 *rread(Fid*), *rwrite(Fid*), *rclunk(Fid*),
90 *rremove(Fid*), *rstat(Fid*), *rwstat(Fid*);
92 char *(*fcalls[])(Fid*) = {
108 char Eperm[] = "permission denied";
109 char Enotdir[] = "not a directory";
110 char Enoauth[] = "no authentication in ramfs";
111 char Enotexist[] = "file does not exist";
112 char Einuse[] = "file in use";
113 char Eexist[] = "file exists";
114 char Eisdir[] = "file is a directory";
115 char Enotowner[] = "not owner";
116 char Eisopen[] = "file already open for I/O";
117 char Excl[] = "exclusive use file already open";
118 char Ename[] = "illegal name";
119 char Eversion[] = "unknown 9P version";
124 notifyf(void *a, char *s)
127 if(strncmp(s, "interrupt", 9) == 0)
133 ramfsmain(int argc, char *argv[])
148 case 'i': /* this is DIFFERENT from normal ramfs; use 1 for both for kernel */
167 error("pipe failed");
172 fd = create("#s/ramfs", OWRITE, 0666);
174 error("create of /srv/ramfs failed");
175 sprint(buf, "%d", p[1]);
176 if(write(fd, buf, strlen(buf)) < 0)
177 error("writing /srv/ramfs");
181 user = atom(getuser());
188 r->perm = DMDIR | 0775;
198 r->name = estrdup(".");
201 fmtinstall('F', fcallfmt);
202 switch(rfork(RFFDG|RFPROC|RFNAMEG|RFNOTEG)){
210 close(mfd[0]); /* don't deadlock if child fails */
211 if(defmnt && mount(srvfd, -1, defmnt, MREPL|MCREATE, "") < 0)
212 error("mount failed: %r");
221 for(f = fids; f; f = f->next)
225 return "message size too small";
226 if(thdr.msize > sizeof mdata)
227 rhdr.msize = sizeof mdata;
229 rhdr.msize = thdr.msize;
230 messagesize = rhdr.msize;
231 if(strncmp(thdr.version, "9P2000", 6) != 0)
233 rhdr.version = "9P2000";
240 return "ramfs: no authentication required";
253 /* no authentication! */
257 rhdr.qid = f->ram->qid;
259 f->user = atom(thdr.uname);
261 f->user = atom("none");
262 if(strcmp(user, "none") == 0)
268 clone(Fid *f, Fid **nf)
272 if(f->ram->busy == 0)
274 *nf = newfid(thdr.newfid);
279 (*nf)->user = f->user; /* no ref count; the leakage is minor */
297 if(rhdr.newfid != rhdr.fid){
301 f = nf; /* walk the new fid */
306 for(i=0; i<thdr.nwname && i<MAXWELEM; i++){
307 if((fram->qid.type & QTDIR) == 0){
316 name = thdr.wname[i];
317 if(strcmp(name, ".") == 0){
320 rhdr.wqid[i] = fram->qid;
323 parent = &ram[fram->parent];
325 if(!perm(f, parent, Pexec)){
330 if(strcmp(name, "..") == 0){
334 for(r=ram; r < &ram[nram]; r++)
335 if(r->busy && r->parent==fram-ram && strcmp(name, r->name)==0){
341 if(i==0 && err == nil)
344 if(nf != nil && (err!=nil || rhdr.nwqid<thdr.nwname)){
345 /* clunk the new fid, which is the one we walked */
349 if(rhdr.nwqid == thdr.nwname) /* update the fid after a successful walk */
369 if(r->qid.type & QTDIR){
376 /* can't remove root; must be able to write parent */
377 if(r->qid.path==0 || !perm(f, &ram[r->parent], Pwrite))
381 trunc = mode & OTRUNC;
383 if(mode==OWRITE || mode==ORDWR || trunc)
384 if(!perm(f, r, Pwrite))
386 if(mode==OREAD || mode==ORDWR)
387 if(!perm(f, r, Pread))
390 if(!perm(f, r, Pexec))
392 if(trunc && (r->perm&DMAPPEND)==0){
400 rhdr.iounit = messagesize-IOHDRSZ;
415 if(f->ram->busy == 0)
417 parent = f->ram - ram;
418 if((f->ram->qid.type&QTDIR) == 0)
420 /* must be able to write parent */
422 if(!perm(f, f->ram, Pwrite))
427 if(strcmp(name, ".")==0 || strcmp(name, "..")==0)
429 for(r=ram; r<&ram[nram]; r++)
430 if(r->busy && parent==r->parent)
431 if(strcmp((char*)name, r->name)==0)
433 for(r=ram; r->busy; r++)
434 if(r == &ram[Nram-1])
435 return "no free ram resources";
437 r->qid.path = ++path;
440 r->qid.type |= QTDIR;
443 r->name = estrdup(name);
445 r->group = f->ram->group;
446 r->muid = f->ram->muid;
448 prm = (prm&~0777) | (f->ram->perm&prm&0777);
450 prm = (prm&(~0777|0111)) | (f->ram->perm&prm&0666);
457 f->ram->mtime = r->atime;
460 rhdr.iounit = messagesize-IOHDRSZ;
462 if(thdr.mode & ORCLOSE)
476 if(f->ram->busy == 0)
483 if(cnt > messagesize) /* shouldn't happen, anyway */
485 if(f->ram->qid.type & QTDIR){
486 for(r=ram+1; off > 0; r++){
487 if(r->busy && r->parent==f->ram-ram)
488 off -= ramstat(r, statbuf, sizeof statbuf);
489 if(r == &ram[nram-1])
492 for(; r<&ram[nram] && n < cnt; r++){
493 if(!r->busy || r->parent!=f->ram-ram)
495 m = ramstat(r, buf+n, cnt-n);
500 rhdr.data = (char*)rdata;
511 rhdr.data = r->data+off;
527 if(r->perm & DMAPPEND)
530 if(r->qid.type & QTDIR)
532 if(off+cnt >= Maxsize) /* sanity check */
533 return "write too big";
534 if(off+cnt > r->ndata)
535 r->data = erealloc(r->data, off+cnt);
537 memset(r->data+r->ndata, 0, off-r->ndata);
538 if(off+cnt > r->ndata)
540 memmove(r->data+off, thdr.data, cnt);
555 memset(&r->qid, 0, sizeof r->qid);
586 if(r->qid.path == 0 || !perm(f, &ram[r->parent], Pwrite))
589 ram[r->parent].mtime = time(0);
597 if(f->ram->busy == 0)
599 rhdr.nstat = ramstat(f->ram, statbuf, sizeof statbuf);
610 if(f->ram->busy == 0)
612 convM2D(thdr.stat, thdr.nstat, &dir, (char*)statbuf);
616 * To change length, must have write permission on file.
619 if(dir.length!=~0 && dir.length!=r->ndata){
620 if(!perm(f, r, Pwrite))
626 * To change name, must have write permission in parent
627 * and name must be unique.
629 if(dir.name[0]!='\0' && strcmp(dir.name, r->name)!=0){
631 if(!perm(f, &ram[r->parent], Pwrite))
634 for(s=ram; s<&ram[nram]; s++)
635 if(s->busy && s->parent==r->parent)
636 if(strcmp(dir.name, s->name)==0)
642 * To change mode, must be owner or group leader.
643 * Because of lack of users file, leader=>group itself.
645 if(dir.mode!=~0 && r->perm!=dir.mode){
646 if(strcmp(f->user, r->user) != 0)
647 if(strcmp(f->user, r->group) != 0)
652 * To change group, must be owner and member of new group,
653 * or leader of current group and leader of new group.
654 * Second case cannot happen, but we check anyway.
656 if(dir.gid[0]!='\0' && strcmp(r->group, dir.gid)!=0){
657 if(strcmp(f->user, r->user) == 0)
658 if(strcmp(f->user, dir.gid) == 0)
660 if(strcmp(f->user, r->group) == 0)
661 if(strcmp(f->user, dir.gid) == 0)
670 dir.mode &= ~DMDIR; /* cannot change dir bit */
671 dir.mode |= r->perm&DMDIR;
674 if(dir.name[0] != '\0'){
676 r->name = estrdup(dir.name);
678 if(dir.gid[0] != '\0')
679 r->group = atom(dir.gid);
681 if(dir.uid[0] != '\0')
682 r->user = atom(dir.uid);
684 if(dir.length!=~0 && dir.length!=r->ndata){
685 r->data = erealloc(r->data, dir.length);
686 if(r->ndata < dir.length)
687 memset(r->data+r->ndata, 0, dir.length-r->ndata);
688 r->ndata = dir.length;
692 r->mtime = dir.mtime;
694 ram[r->parent].mtime = time(0);
699 ramstat(Ram *r, uchar *buf, uint nbuf)
706 dir.length = r->ndata;
710 dir.atime = r->atime;
711 dir.mtime = r->mtime;
712 return convD2M(&dir, buf, nbuf);
721 for(f = fids; f; f = f->next)
724 else if(!ff && !f->busy)
730 f = emalloc(sizeof *f);
746 while((n = read9pmsg(mfd[0], mdata, messagesize)) != 0){
748 error("mount read: %r");
749 if(convM2S(mdata, n, &thdr) != n)
750 error("convM2S format error: %r");
753 fprint(2, "ramfs %d:<-%F\n", pid, &thdr);
755 if(!fcalls[thdr.type])
756 err = "bad fcall type";
758 err = (*fcalls[thdr.type])(newfid(thdr.fid));
763 rhdr.type = thdr.type + 1;
768 fprint(2, "ramfs %d:->%F\n", pid, &rhdr);/**/
769 n = convS2M(&rhdr, mdata, messagesize);
771 error("convS2M error on write");
772 if(write(mfd[1], mdata, n) != n)
773 error("mount write");
778 perm(Fid *f, Ram *r, int p)
780 if((p*Pother) & r->perm)
782 if(strcmp(f->user, r->group)==0 && ((p*Pgroup) & r->perm))
784 if(strcmp(f->user, r->user)==0 && ((p*Powner) & r->perm))
796 error("out of memory");
802 erealloc(void *p, ulong n)
806 error("out of memory");
819 error("out of memory");
827 fprint(2, "usage: %s [-is] [-m mountpoint]\n", argv0);
832 * Custom allocators to avoid malloc overheads on small objects.
833 * We never free these. (See below.)
835 typedef struct Stringtab Stringtab;
847 t = malloc(64*sizeof(Stringtab));
849 sysfatal("out of memory");
866 sysfatal("strdup big string");
871 sysfatal("out of memory");
882 * Return a uniquely allocated copy of a string.
883 * Don't free these -- they stay in the table for the
884 * next caller who wants that particular string.
885 * String comparison can be done with pointer comparison
886 * if you know both strings are atoms.
888 static Stringtab *stab[1024];
897 for(p=(uchar*)s; *p; p++)
908 h = hash(str) % nelem(stab);
909 for(tab=stab[h]; tab; tab=tab->link)
910 if(strcmp(str, tab->str) == 0)
914 tab->str = xstrdup(str);