12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574 |
- #include "all.h"
- #include "io.h"
- Filsys*
- fsstr(char *p)
- {
- Filsys *fs;
- for(fs=filsys; fs->name; fs++)
- if(strcmp(fs->name, p) == 0)
- return fs;
- return 0;
- }
- Filsys*
- dev2fs(Device *dev)
- {
- Filsys *fs;
- for(fs=filsys; fs->name; fs++)
- if(fs->dev == dev)
- return fs;
- return 0;
- }
- /*
- * allocate 'count' contiguous channels
- * of type 'type' and return pointer to base
- */
- Chan*
- chaninit(int type, int count, int data)
- {
- uchar *p;
- Chan *cp, *icp;
- int i;
- p = ialloc(count * (sizeof(Chan)+data), 0);
- icp = (Chan*)p;
- for(i=0; i<count; i++) {
- cp = (Chan*)p;
- cp->next = chans;
- chans = cp;
- cp->type = type;
- cp->chan = cons.chano;
- cons.chano++;
- strncpy(cp->whoname, "<none>", sizeof(cp->whoname));
- dofilter(&cp->work, C0a, C0b, 1);
- dofilter(&cp->rate, C0a, C0b, 1000);
- wlock(&cp->reflock);
- wunlock(&cp->reflock);
- rlock(&cp->reflock);
- runlock(&cp->reflock);
- p += sizeof(Chan);
- if(data){
- cp->pdata = p;
- p += data;
- }
- }
- return icp;
- }
- void
- fileinit(Chan *cp)
- {
- File *f, *prev;
- Tlock *t;
- int h;
- loop:
- lock(&flock);
- for(h=0; h<nelem(flist); h++) {
- for(prev=0,f=flist[h]; f; prev=f,f=f->next) {
- if(f->cp != cp)
- continue;
- if(prev) {
- prev->next = f->next;
- f->next = flist[h];
- flist[h] = f;
- }
- goto out;
- }
- }
- unlock(&flock);
- return;
- out:
- flist[h] = f->next;
- unlock(&flock);
- qlock(f);
- if(t = f->tlock) {
- if(t->file == f)
- t->time = 0; /* free the lock */
- f->tlock = 0;
- }
- if(f->open & FREMOV)
- doremove(f, 0);
- freewp(f->wpath);
- f->open = 0;
- f->cp = 0;
- qunlock(f);
- goto loop;
- }
- #define NOFID (ulong)~0
- /*
- * returns a locked file structure
- */
- File*
- filep(Chan *cp, ulong fid, int flag)
- {
- File *f;
- int h;
- if(fid == NOFID)
- return 0;
- h = (long)cp + fid;
- if(h < 0)
- h = ~h;
- h = h % nelem(flist);
- loop:
- lock(&flock);
- for(f=flist[h]; f; f=f->next)
- if(f->fid == fid && f->cp == cp){
- /*
- * Already in use is an error
- * when called from attach or clone (walk
- * in 9P2000). The console uses FID[12] and
- * never clunks them so catch that case.
- */
- if(flag == 0 || cp == cons.chan)
- goto out;
- unlock(&flock);
- return 0;
- }
- if(flag) {
- f = newfp();
- if(f) {
- f->fid = fid;
- f->cp = cp;
- f->wpath = 0;
- f->tlock = 0;
- f->doffset = 0;
- f->dslot = 0;
- f->auth = 0;
- f->next = flist[h];
- flist[h] = f;
- goto out;
- }
- }
- unlock(&flock);
- return 0;
- out:
- unlock(&flock);
- qlock(f);
- if(f->fid == fid && f->cp == cp)
- return f;
- qunlock(f);
- goto loop;
- }
- /*
- * always called with flock locked
- */
- File*
- newfp(void)
- {
- static first;
- File *f;
- int start, i;
- i = first;
- start = i;
- do {
- f = &files[i];
- i++;
- if(i >= conf.nfile)
- i = 0;
- if(f->cp)
- continue;
- first = i;
- return f;
- } while(i != start);
- print("out of files\n");
- return 0;
- }
- void
- freefp(File *fp)
- {
- Chan *cp;
- File *f, *prev;
- int h;
- if(!fp || !(cp = fp->cp))
- return;
- h = (long)cp + fp->fid;
- if(h < 0)
- h = ~h;
- h = h % nelem(flist);
- lock(&flock);
- for(prev=0,f=flist[h]; f; prev=f,f=f->next)
- if(f == fp) {
- if(prev)
- prev->next = f->next;
- else
- flist[h] = f->next;
- break;
- }
- fp->cp = 0;
- unlock(&flock);
- }
- int
- iaccess(File *f, Dentry *d, int m)
- {
- /* uid none gets only other permissions */
- if(f->uid == 0)
- goto doother;
- /*
- * owner
- */
- if(f->uid == d->uid)
- if((m<<6) & d->mode)
- return 0;
- /*
- * group membership
- */
- if(ingroup(f->uid, d->gid))
- if((m<<3) & d->mode)
- return 0;
- doother:
- /*
- * other
- */
- if(m & d->mode) {
- if((d->mode & DDIR) && (m == DEXEC))
- return 0;
- if(!ingroup(f->uid, 9999))
- return 0;
- }
- /*
- * various forms of superuser
- */
- if(wstatallow)
- return 0;
- if(duallow != 0 && duallow == f->uid)
- if((d->mode & DDIR) && (m == DREAD || m == DEXEC))
- return 0;
- return 1;
- }
- Tlock*
- tlocked(Iobuf *p, Dentry *d)
- {
- Tlock *t, *t1;
- long qpath, tim;
- Device *dev;
- tim = toytime();
- qpath = d->qid.path;
- dev = p->dev;
- again:
- t1 = 0;
- for(t=tlocks+NTLOCK-1; t>=tlocks; t--) {
- if(t->qpath == qpath)
- if(t->time >= tim)
- if(t->dev == dev)
- return nil; /* its locked */
- if(t1 != nil && t->time == 0)
- t1 = t; /* remember free lock */
- }
- if(t1 == 0) {
- // reclaim old locks
- lock(&tlocklock);
- for(t=tlocks+NTLOCK-1; t>=tlocks; t--)
- if(t->time < tim) {
- t->time = 0;
- t1 = t;
- }
- unlock(&tlocklock);
- }
- if(t1) {
- lock(&tlocklock);
- if(t1->time != 0) {
- unlock(&tlocklock);
- goto again;
- }
- t1->dev = dev;
- t1->qpath = qpath;
- t1->time = tim + TLOCK;
- unlock(&tlocklock);
- }
- /* botch
- * out of tlock nodes simulates
- * a locked file
- */
- return t1;
- }
- Wpath*
- newwp(void)
- {
- static int si = 0;
- int i;
- Wpath *w, *sw, *ew;
- i = si + 1;
- if(i < 0 || i >= conf.nwpath)
- i = 0;
- si = i;
- sw = &wpaths[i];
- ew = &wpaths[conf.nwpath];
- for(w=sw;;) {
- w++;
- if(w >= ew)
- w = &wpaths[0];
- if(w == sw) {
- print("out of wpaths\n");
- return 0;
- }
- if(w->refs)
- continue;
- lock(&wpathlock);
- if(w->refs) {
- unlock(&wpathlock);
- continue;
- }
- w->refs = 1;
- w->up = 0;
- unlock(&wpathlock);
- return w;
- }
- }
- void
- freewp(Wpath *w)
- {
- lock(&wpathlock);
- for(; w; w=w->up)
- w->refs--;
- unlock(&wpathlock);
- }
- long
- qidpathgen(Device *dev)
- {
- Iobuf *p;
- Superb *sb;
- long path;
- p = getbuf(dev, superaddr(dev), Bread|Bmod);
- if(!p || checktag(p, Tsuper, QPSUPER))
- panic("newqid: super block");
- sb = (Superb*)p->iobuf;
- sb->qidgen++;
- path = sb->qidgen;
- putbuf(p);
- return path;
- }
- void
- buffree(Device *dev, long addr, int d)
- {
- Iobuf *p;
- long a;
- int i;
- if(!addr)
- return;
- if(d > 0) {
- d--;
- p = getbuf(dev, addr, Bread);
- if(p) {
- for(i=INDPERBUF-1; i>=0; i--) {
- a = ((long*)p->iobuf)[i];
- buffree(dev, a, d);
- }
- putbuf(p);
- }
- }
- /*
- * stop outstanding i/o
- */
- p = getbuf(dev, addr, Bprobe);
- if(p) {
- p->flags &= ~(Bmod|Bimm);
- putbuf(p);
- }
- /*
- * dont put written worm
- * blocks into free list
- */
- if(dev->type == Devcw) {
- i = cwfree(dev, addr);
- if(i)
- return;
- }
- p = getbuf(dev, superaddr(dev), Bread|Bmod);
- if(!p || checktag(p, Tsuper, QPSUPER))
- panic("buffree: super block");
- addfree(dev, addr, (Superb*)p->iobuf);
- putbuf(p);
- }
- long
- bufalloc(Device *dev, int tag, long qid, int uid)
- {
- Iobuf *bp, *p;
- Superb *sb;
- long a;
- int n;
- p = getbuf(dev, superaddr(dev), Bread|Bmod);
- if(!p || checktag(p, Tsuper, QPSUPER)) {
- print("bufalloc: super block\n");
- if(p)
- putbuf(p);
- return 0;
- }
- sb = (Superb*)p->iobuf;
- loop:
- n = --sb->fbuf.nfree;
- sb->tfree--;
- if(n < 0 || n >= FEPERBUF) {
- print("bufalloc: %Z: bad freelist\n", dev);
- n = 0;
- sb->fbuf.free[0] = 0;
- }
- a = sb->fbuf.free[n];
- if(n <= 0) {
- if(a == 0) {
- sb->tfree = 0;
- sb->fbuf.nfree = 1;
- if(dev->type == Devcw) {
- n = uid;
- if(n < 0 || n >= nelem(growacct))
- n = 0;
- growacct[n]++;
- if(cwgrow(dev, sb, uid))
- goto loop;
- }
- putbuf(p);
- print("fs %Z full uid=%d\n", dev, uid);
- return 0;
- }
- bp = getbuf(dev, a, Bread);
- if(!bp || checktag(bp, Tfree, QPNONE)) {
- if(bp)
- putbuf(bp);
- putbuf(p);
- return 0;
- }
- sb->fbuf = *(Fbuf*)bp->iobuf;
- putbuf(bp);
- }
- bp = getbuf(dev, a, Bmod);
- memset(bp->iobuf, 0, RBUFSIZE);
- settag(bp, tag, qid);
- if(tag == Tind1 || tag == Tind2 || tag == Tdir)
- bp->flags |= Bimm;
- putbuf(bp);
- putbuf(p);
- return a;
- }
- /*
- * what are legal characters in a name?
- * only disallow control characters.
- * a) utf avoids control characters.
- * b) '/' may not be the separator
- */
- int
- checkname(char *n)
- {
- int i, c;
- for(i=0; i<NAMELEN; i++) {
- c = *n & 0xff;
- if(c == 0) {
- if(i == 0)
- return 1;
- memset(n, 0, NAMELEN-i);
- return 0;
- }
- if(c <= 040)
- return 1;
- n++;
- }
- return 1; /* too long */
- }
- void
- addfree(Device *dev, long addr, Superb *sb)
- {
- int n;
- Iobuf *p;
- n = sb->fbuf.nfree;
- if(n < 0 || n > FEPERBUF)
- panic("addfree: bad freelist");
- if(n >= FEPERBUF) {
- p = getbuf(dev, addr, Bmod|Bimm);
- if(p == 0)
- panic("addfree: getbuf");
- *(Fbuf*)p->iobuf = sb->fbuf;
- settag(p, Tfree, QPNONE);
- putbuf(p);
- n = 0;
- }
- sb->fbuf.free[n++] = addr;
- sb->fbuf.nfree = n;
- sb->tfree++;
- if(addr >= sb->fsize)
- sb->fsize = addr+1;
- }
- static int
- Yfmt(Fmt* fmt)
- {
- Chan *cp;
- char s[20];
- cp = va_arg(fmt->args, Chan*);
- sprint(s, "C%d.%.3d", cp->type, cp->chan);
- return fmtstrcpy(fmt, s);
- }
- static int
- Zfmt(Fmt* fmt)
- {
- Device *d;
- int c, c1;
- char s[100];
- d = va_arg(fmt->args, Device*);
- if(d == 0) {
- sprint(s, "Z***");
- goto out;
- }
- switch(d->type) {
- default:
- sprint(s, "D%d", d->type);
- break;
- case Devwren:
- c = 'w';
- goto d1;
- case Devide:
- c = 'h';
- goto d1;
- case Devworm:
- c = 'r';
- goto d1;
- case Devlworm:
- c = 'l';
- goto d1;
- d1:
- if(d->wren.ctrl == 0 && d->wren.lun == 0)
- sprint(s, "%c%d", c, d->wren.targ);
- else
- sprint(s, "%c%d.%d.%d", c, d->wren.ctrl, d->wren.targ, d->wren.lun);
- break;
- case Devmcat:
- c = '(';
- c1 = ')';
- goto d2;
- case Devmlev:
- c = '[';
- c1 = ']';
- goto d2;
- case Devmirr:
- c = '{';
- c1 = '}';
- d2:
- if(d->cat.first == d->cat.last)
- sprint(s, "%c%Z%c", c, d->cat.first, c1);
- else
- if(d->cat.first->link == d->cat.last)
- sprint(s, "%c%Z%Z%c", c, d->cat.first, d->cat.last, c1);
- else
- sprint(s, "%c%Z-%Z%c", c, d->cat.first, d->cat.last, c1);
- break;
- case Devro:
- sprint(s, "o%Z%Z", d->ro.parent->cw.c, d->ro.parent->cw.w);
- break;
- case Devcw:
- sprint(s, "c%Z%Z", d->cw.c, d->cw.w);
- break;
- case Devjuke:
- sprint(s, "j%Z%Z", d->j.j, d->j.m);
- break;
- case Devfworm:
- sprint(s, "f%Z", d->fw.fw);
- break;
- case Devpart:
- sprint(s, "p(%Z)%ld.%ld",
- d->part.d, d->part.base, d->part.size);
- break;
- case Devswab:
- sprint(s, "x%Z", d->swab.d);
- break;
- case Devnone:
- sprint(s, "n");
- break;
- }
- out:
- return fmtstrcpy(fmt, s);
- }
- static int
- Wfmt(Fmt* fmt)
- {
- Filter* a;
- char s[30];
- a = va_arg(fmt->args, Filter*);
- sprint(s, "%lud", fdf(a->filter, a->c3*a->c1));
- return fmtstrcpy(fmt, s);
- }
- static int
- Gfmt(Fmt* fmt)
- {
- int t;
- char *s;
- t = va_arg(fmt->args, int);
- s = "<badtag>";
- if(t >= 0 && t < MAXTAG)
- s = tagnames[t];
- return fmtstrcpy(fmt, s);
- }
- static int
- Efmt(Fmt* fmt)
- {
- char s[64];
- uchar *p;
- p = va_arg(fmt->args, uchar*);
- sprint(s, "%.2ux%.2ux%.2ux%.2ux%.2ux%.2ux",
- p[0], p[1], p[2], p[3], p[4], p[5]);
- return fmtstrcpy(fmt, s);
- }
- static int
- Ifmt(Fmt* fmt)
- {
- char s[64];
- uchar *p;
- p = va_arg(fmt->args, uchar*);
- sprint(s, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
- return fmtstrcpy(fmt, s);
- }
- void
- formatinit(void)
- {
- quotefmtinstall();
- fmtinstall('Y', Yfmt); /* print channels */
- fmtinstall('Z', Zfmt); /* print devices */
- fmtinstall('W', Wfmt); /* print filters */
- fmtinstall('G', Gfmt); /* print tags */
- fmtinstall('T', Tfmt); /* print times */
- fmtinstall('E', Efmt); /* print ether addresses */
- fmtinstall('I', Ifmt); /* print ip addresses */
- }
- void
- rootream(Device *dev, long addr)
- {
- Iobuf *p;
- Dentry *d;
- p = getbuf(dev, addr, Bmod|Bimm);
- memset(p->iobuf, 0, RBUFSIZE);
- settag(p, Tdir, QPROOT);
- d = getdir(p, 0);
- strcpy(d->name, "/");
- d->uid = -1;
- d->gid = -1;
- d->mode = DALLOC | DDIR |
- ((DREAD|DEXEC) << 6) |
- ((DREAD|DEXEC) << 3) |
- ((DREAD|DEXEC) << 0);
- d->qid = QID9P1(QPROOT|QPDIR,0);
- d->atime = time();
- d->mtime = d->atime;
- d->muid = 0;
- putbuf(p);
- }
- void
- superream(Device *dev, long addr)
- {
- Iobuf *p;
- Superb *s;
- long i;
- p = getbuf(dev, addr, Bmod|Bimm);
- memset(p->iobuf, 0, RBUFSIZE);
- settag(p, Tsuper, QPSUPER);
- s = (Superb*)p->iobuf;
- s->fstart = 2;
- s->fsize = devsize(dev);
- s->fbuf.nfree = 1;
- s->qidgen = 10;
- for(i=s->fsize-1; i>=addr+2; i--)
- addfree(dev, i, s);
- putbuf(p);
- }
- struct
- {
- Lock;
- Msgbuf *smsgbuf;
- Msgbuf *lmsgbuf;
- } msgalloc;
- /*
- * pre-allocate some message buffers at boot time.
- * if this supply is exhausted, more will be allocated as needed.
- */
- void
- mbinit(void)
- {
- Msgbuf *mb;
- Rabuf *rb;
- int i;
- lock(&msgalloc);
- unlock(&msgalloc);
- msgalloc.lmsgbuf = 0;
- msgalloc.smsgbuf = 0;
- for(i=0; i<conf.nlgmsg; i++) {
- mb = ialloc(sizeof(Msgbuf), 0);
- if(1)
- mb->xdata = ialloc(LARGEBUF+256, 256);
- else
- mb->xdata = ialloc(LARGEBUF+OFFMSG, LINESIZE);
- mb->flags = LARGE;
- mb->free = 0;
- mbfree(mb);
- cons.nlarge++;
- }
- for(i=0; i<conf.nsmmsg; i++) {
- mb = ialloc(sizeof(Msgbuf), 0);
- if(1)
- mb->xdata = ialloc(SMALLBUF+256, 256);
- else
- mb->xdata = ialloc(SMALLBUF+OFFMSG, LINESIZE);
- mb->flags = 0;
- mb->free = 0;
- mbfree(mb);
- cons.nsmall++;
- }
- memset(mballocs, 0, sizeof(mballocs));
- lock(&rabuflock);
- unlock(&rabuflock);
- rabuffree = 0;
- for(i=0; i<1000; i++) {
- rb = ialloc(sizeof(*rb), 0);
- rb->link = rabuffree;
- rabuffree = rb;
- }
- }
- Msgbuf*
- mballoc(int count, Chan *cp, int category)
- {
- Msgbuf *mb;
- ilock(&msgalloc);
- if(count > SMALLBUF) {
- if(count > LARGEBUF)
- panic("msgbuf count");
- mb = msgalloc.lmsgbuf;
- if(mb == 0) {
- mb = ialloc(sizeof(Msgbuf), 0);
- if(1)
- mb->xdata = ialloc(LARGEBUF+256, 256);
- else
- mb->xdata = ialloc(LARGEBUF+OFFMSG, LINESIZE);
- mb->free = 0;
- cons.nlarge++;
- } else
- msgalloc.lmsgbuf = mb->next;
- mb->flags = LARGE;
- } else {
- mb = msgalloc.smsgbuf;
- if(mb == 0) {
- mb = ialloc(sizeof(Msgbuf), 0);
- if(1)
- mb->xdata = ialloc(SMALLBUF+256, 256);
- else
- mb->xdata = ialloc(SMALLBUF+OFFMSG, LINESIZE);
- mb->free = 0;
- cons.nsmall++;
- } else
- msgalloc.smsgbuf = mb->next;
- mb->flags = 0;
- }
- mballocs[category]++;
- iunlock(&msgalloc);
- mb->count = count;
- mb->chan = cp;
- mb->next = 0;
- mb->param = 0;
- mb->category = category;
- if(1)
- mb->data = mb->xdata+256;
- else
- mb->data = mb->xdata+OFFMSG;
- mb->free = 0;
- return mb;
- }
- void
- mbfree(Msgbuf *mb)
- {
- if(mb == nil)
- return;
- if(mb->flags & BTRACE)
- print("mbfree: BTRACE cat=%d flags=%ux, caller 0x%lux\n",
- mb->category, mb->flags, getcallerpc(&mb));
- /*
- * drivers which perform non cache coherent DMA manage their own buffer
- * pool of uncached buffers and provide their own free routine.
- * this is provided mainly for ethernet drivers ported from cpu kernel.
- */
- if(mb->flags&Mbrcvbuf) {
- if (mb->free == nil)
- panic("freeb: nil mb->free");
- (*mb->free)(mb);
- return;
- }
- if(mb->flags & FREE)
- panic("mbfree already free");
- ilock(&msgalloc);
- mballocs[mb->category]--;
- mb->flags |= FREE;
- if(mb->flags & LARGE) {
- mb->next = msgalloc.lmsgbuf;
- msgalloc.lmsgbuf = mb;
- } else {
- mb->next = msgalloc.smsgbuf;
- msgalloc.smsgbuf = mb;
- }
- mb->data = 0;
- mb->free = 0;
- iunlock(&msgalloc);
- }
- /*
- * returns 1 if n is prime
- * used for adjusting lengths
- * of hashing things.
- * there is no need to be clever
- */
- int
- prime(long n)
- {
- long i;
- if((n%2) == 0)
- return 0;
- for(i=3;; i+=2) {
- if((n%i) == 0)
- return 0;
- if(i*i >= n)
- return 1;
- }
- }
- char*
- getwd(char *word, char *line)
- {
- int c, n;
- while(*line == ' ')
- line++;
- for(n=0; n<80; n++) {
- c = *line;
- if(c == ' ' || c == 0 || c == '\n')
- break;
- line++;
- *word++ = c;
- }
- *word = 0;
- return line;
- }
- void
- hexdump(void *a, int n)
- {
- char s1[30], s2[4];
- uchar *p;
- int i;
- p = a;
- s1[0] = 0;
- for(i=0; i<n; i++) {
- sprint(s2, " %.2ux", p[i]);
- strcat(s1, s2);
- if((i&7) == 7) {
- print("%s\n", s1);
- s1[0] = 0;
- }
- }
- if(s1[0])
- print("%s\n", s1);
- }
- void*
- recv(Queue *q, int)
- {
- User *p;
- void *a;
- int i, c;
- long s;
- if(q == 0)
- panic("recv null q");
- for(;;) {
- lock(q);
- c = q->count;
- if(c > 0) {
- i = q->loc;
- a = q->args[i];
- i++;
- if(i >= q->size)
- i = 0;
- q->loc = i;
- q->count = c-1;
- p = q->whead;
- if(p) {
- q->whead = p->qnext;
- if(q->whead == 0)
- q->wtail = 0;
- ready(p);
- }
- unlock(q);
- return a;
- }
- p = q->rtail;
- if(p == 0)
- q->rhead = u;
- else
- p->qnext = u;
- q->rtail = u;
- u->qnext = 0;
- s = splhi();
- u->state = Recving;
- unlock(q);
- sched();
- splx(s);
- }
- return 0;
- }
- void
- send(Queue *q, void *a)
- {
- User *p;
- int i, c;
- long s;
- if(q == 0)
- panic("send null q");
- for(;;) {
- lock(q);
- c = q->count;
- if(c < q->size) {
- i = q->loc + c;
- if(i >= q->size)
- i -= q->size;
- q->args[i] = a;
- q->count = c+1;
- p = q->rhead;
- if(p) {
- q->rhead = p->qnext;
- if(q->rhead == 0)
- q->rtail = 0;
- ready(p);
- }
- unlock(q);
- return;
- }
- p = q->wtail;
- if(p == 0)
- q->whead = u;
- else
- p->qnext = u;
- q->wtail = u;
- u->qnext = 0;
- s = splhi();
- u->state = Sending;
- unlock(q);
- sched();
- splx(s);
- }
- }
- Queue*
- newqueue(int size)
- {
- Queue *q;
- q = ialloc(sizeof(Queue) + (size-1)*sizeof(void*), 0);
- q->size = size;
- lock(q);
- unlock(q);
- return q;
- }
- no(void*)
- {
- return 0;
- }
- int
- devread(Device *d, long b, void *c)
- {
- int e;
- loop:
- switch(d->type)
- {
- case Devcw:
- return cwread(d, b, c);
- case Devjuke:
- d = d->j.m;
- goto loop;
- case Devro:
- return roread(d, b, c);
- case Devwren:
- return wrenread(d, b, c);
- case Devide:
- return ideread(d, b, c);
- case Devworm:
- case Devlworm:
- return wormread(d, b, c);
- case Devfworm:
- return fwormread(d, b, c);
- case Devmcat:
- return mcatread(d, b, c);
- case Devmlev:
- return mlevread(d, b, c);
- case Devmirr:
- return mirrread(d, b, c);
- case Devpart:
- return partread(d, b, c);
- case Devswab:
- e = devread(d->swab.d, b, c);
- if(e == 0)
- swab(c, 0);
- return e;
- case Devnone:
- print("read from device none(%ld)\n", b);
- return 1;
- }
- panic("illegal device in read: %Z %ld", d, b);
- return 1;
- }
- int
- devwrite(Device *d, long b, void *c)
- {
- int e;
- /*
- * set readonly to non-0 to prevent all writes;
- * mainly for trying dangerous experiments.
- */
- if (readonly)
- return 0;
- loop:
- switch(d->type)
- {
- case Devcw:
- return cwwrite(d, b, c);
- case Devjuke:
- d = d->j.m;
- goto loop;
- case Devro:
- print("write to ro device %Z(%ld)\n", d, b);
- return 1;
- case Devwren:
- return wrenwrite(d, b, c);
- case Devide:
- return idewrite(d, b, c);
- case Devworm:
- case Devlworm:
- return wormwrite(d, b, c);
- case Devfworm:
- return fwormwrite(d, b, c);
- case Devmcat:
- return mcatwrite(d, b, c);
- case Devmlev:
- return mlevwrite(d, b, c);
- case Devmirr:
- return mirrwrite(d, b, c);
- case Devpart:
- return partwrite(d, b, c);
- case Devswab:
- swab(c, 1);
- e = devwrite(d->swab.d, b, c);
- swab(c, 0);
- return e;
- case Devnone:
- /* checktag() can generate blocks with type devnone */
- if (0) {
- print("write to device none(%ld)\n", b);
- return 1;
- }
- return 0;
- }
- panic("illegal device in write: %Z %ld", d, b);
- return 1;
- }
- long
- devsize(Device *d)
- {
- loop:
- switch(d->type)
- {
- case Devcw:
- case Devro:
- return cwsize(d);
- case Devjuke:
- d = d->j.m;
- goto loop;
- case Devwren:
- return wrensize(d);
- case Devide:
- return atasize(d);
- case Devworm:
- case Devlworm:
- return wormsize(d);
- case Devfworm:
- return fwormsize(d);
- case Devmcat:
- return mcatsize(d);
- case Devmlev:
- return mlevsize(d);
- case Devmirr:
- return mirrsize(d);
- case Devpart:
- return partsize(d);
- case Devswab:
- d = d->swab.d;
- goto loop;
- }
- panic("illegal device in dev_size: %Z", d);
- return 0;
- }
- long
- superaddr(Device *d)
- {
- loop:
- switch(d->type) {
- default:
- return SUPER_ADDR;
- case Devcw:
- case Devro:
- return cwsaddr(d);
- case Devswab:
- d = d->swab.d;
- goto loop;
- }
- }
- long
- getraddr(Device *d)
- {
- loop:
- switch(d->type) {
- default:
- return ROOT_ADDR;
- case Devcw:
- case Devro:
- return cwraddr(d);
- case Devswab:
- d = d->swab.d;
- goto loop;
- }
- }
- void
- devream(Device *d, int top)
- {
- Device *l;
- loop:
- print(" devream: %Z %d\n", d, top);
- switch(d->type) {
- default:
- print("ream: unknown dev type %Z\n", d);
- return;
- case Devcw:
- devream(d->cw.w, 0);
- devream(d->cw.c, 0);
- if(top) {
- wlock(&mainlock);
- cwream(d);
- wunlock(&mainlock);
- }
- devinit(d);
- return;
- case Devfworm:
- devream(d->fw.fw, 0);
- fwormream(d);
- break;
- case Devpart:
- devream(d->part.d, 0);
- break;
- case Devmlev:
- case Devmcat:
- case Devmirr:
- for(l=d->cat.first; l; l=l->link)
- devream(l, 0);
- break;
- case Devjuke:
- case Devworm:
- case Devlworm:
- case Devwren:
- case Devide:
- break;
- case Devswab:
- d = d->swab.d;
- goto loop;
- }
- devinit(d);
- if(top) {
- wlock(&mainlock);
- rootream(d, ROOT_ADDR);
- superream(d, SUPER_ADDR);
- wunlock(&mainlock);
- }
- }
- void
- devrecover(Device *d)
- {
- loop:
- print("recover: %Z\n", d);
- switch(d->type) {
- default:
- print("recover: unknown dev type %Z\n", d);
- return;
- case Devcw:
- wlock(&mainlock); /* recover */
- cwrecover(d);
- wunlock(&mainlock);
- break;
- case Devswab:
- d = d->swab.d;
- goto loop;
- }
- }
- void
- devinit(Device *d)
- {
- loop:
- if(d->init)
- return;
- d->init = 1;
- print(" devinit %Z\n", d);
- switch(d->type) {
- default:
- print("devinit unknown device %Z\n", d);
- return;
- case Devro:
- cwinit(d->ro.parent);
- break;
- case Devcw:
- cwinit(d);
- break;
- case Devjuke:
- jukeinit(d);
- break;
- case Devwren:
- wreninit(d);
- break;
- case Devide:
- ideinit(d);
- break;
- case Devworm:
- case Devlworm:
- break;
- case Devfworm:
- fworminit(d);
- break;
- case Devmcat:
- mcatinit(d);
- break;
- case Devmlev:
- mlevinit(d);
- break;
- case Devmirr:
- mirrinit(d);
- break;
- case Devpart:
- partinit(d);
- break;
- case Devswab:
- d = d->swab.d;
- goto loop;
- case Devnone:
- print("devinit of Devnone\n");
- break;
- }
- }
- void
- swab2(void *c)
- {
- uchar *p;
- int t;
- p = c;
- t = p[0];
- p[0] = p[1];
- p[1] = t;
- }
- void
- swab4(void *c)
- {
- uchar *p;
- int t;
- p = c;
- t = p[0];
- p[0] = p[3];
- p[3] = t;
- t = p[1];
- p[1] = p[2];
- p[2] = t;
- }
- /*
- * swab a block
- * flag = 0 -- convert from foreign to native
- * flag = 1 -- convert from native to foreign
- */
- void
- swab(void *c, int flag)
- {
- uchar *p;
- Tag *t;
- int i, j;
- Dentry *d;
- Cache *h;
- Bucket *b;
- Superb *s;
- Fbuf *f;
- long *l;
- /* swab the tag */
- p = (uchar*)c;
- t = (Tag*)(p + BUFSIZE);
- if(!flag) {
- swab2(&t->pad);
- swab2(&t->tag);
- swab4(&t->path);
- }
- /* swab each block type */
- switch(t->tag) {
- default:
- print("no swab for tag=%G rw=%d\n", t->tag, flag);
- for(j=0; j<16; j++)
- print(" %.2x", p[BUFSIZE+j]);
- print("\n");
- for(i=0; i<16; i++) {
- print("%.4x", i*16);
- for(j=0; j<16; j++)
- print(" %.2x", p[i*16+j]);
- print("\n");
- }
- panic("swab");
- break;
- case Tsuper:
- s = (Superb*)p;
- swab4(&s->fbuf.nfree);
- for(i=0; i<FEPERBUF; i++)
- swab4(&s->fbuf.free[i]);
- swab4(&s->fstart);
- swab4(&s->fsize);
- swab4(&s->tfree);
- swab4(&s->qidgen);
- swab4(&s->cwraddr);
- swab4(&s->roraddr);
- swab4(&s->last);
- swab4(&s->next);
- break;
- case Tdir:
- for(i=0; i<DIRPERBUF; i++) {
- d = (Dentry*)p + i;
- swab2(&d->uid);
- swab2(&d->gid);
- swab2(&d->mode);
- swab2(&d->muid);
- swab4(&d->qid.path);
- swab4(&d->qid.version);
- swab4(&d->size);
- for(j=0; j<NDBLOCK; j++)
- swab4(&d->dblock[j]);
- swab4(&d->iblock);
- swab4(&d->diblock);
- swab4(&d->atime);
- swab4(&d->mtime);
- }
- break;
- case Tind1:
- case Tind2:
- l = (long*)p;
- for(i=0; i<INDPERBUF; i++) {
- swab4(l);
- l++;
- }
- break;
- case Tfree:
- f = (Fbuf*)p;
- swab4(&f->nfree);
- for(i=0; i<FEPERBUF; i++)
- swab4(&f->free[i]);
- break;
- case Tbuck:
- for(i=0; i<BKPERBLK; i++) {
- b = (Bucket*)p + i;
- swab4(&b->agegen);
- for(j=0; j<CEPERBK; j++) {
- swab2(&b->entry[j].age);
- swab2(&b->entry[j].state);
- swab4(&b->entry[j].waddr);
- }
- }
- break;
- case Tcache:
- h = (Cache*)p;
- swab4(&h->maddr);
- swab4(&h->msize);
- swab4(&h->caddr);
- swab4(&h->csize);
- swab4(&h->fsize);
- swab4(&h->wsize);
- swab4(&h->wmax);
- swab4(&h->sbaddr);
- swab4(&h->cwraddr);
- swab4(&h->roraddr);
- swab4(&h->toytime);
- swab4(&h->time);
- break;
- case Tnone: // unitialized
- case Tfile: // someone elses problem
- case Tvirgo: // bit map -- all bytes
- case Tconfig: // configuration string -- all bytes
- break;
- }
- /* swab the tag */
- if(flag) {
- swab2(&t->pad);
- swab2(&t->tag);
- swab4(&t->path);
- }
- }
|