#include "u.h" #include "../port/lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "io.h" #include "../port/error.h" /* * definitions from the old 9P. we need these because sac files * are encoded using the old definitions. */ #define NAMELEN 28 #define CHDIR 0x80000000 /* mode bit for directories */ #define CHAPPEND 0x40000000 /* mode bit for append only files */ #define CHEXCL 0x20000000 /* mode bit for exclusive use files */ #define CHMOUNT 0x10000000 /* mode bit for mounted channel */ #define CHREAD 0x4 /* mode bit for read permission */ #define CHWRITE 0x2 /* mode bit for write permission */ #define CHEXEC 0x1 /* mode bit for execute permission */ enum { OPERM = 0x3, /* mask of all permission types in open mode */ Nram = 512, CacheSize = 20, OffsetSize = 4, /* size in bytes of an offset */ }; typedef struct SacPath SacPath; typedef struct Sac Sac; typedef struct SacHeader SacHeader; typedef struct SacDir SacDir; typedef struct Cache Cache; enum { Magic = 0x5acf5, }; struct SacDir { char name[NAMELEN]; char uid[NAMELEN]; char gid[NAMELEN]; uchar qid[4]; uchar mode[4]; uchar atime[4]; uchar mtime[4]; uchar length[8]; uchar blocks[8]; }; struct SacHeader { uchar magic[4]; uchar length[8]; uchar blocksize[4]; uchar md5[16]; }; struct Sac { SacDir; SacPath *path; }; struct SacPath { Ref; SacPath *up; long blocks; int entry; int nentry; }; struct Cache { long block; ulong age; uchar *data; }; enum { Pexec = 1, Pwrite = 2, Pread = 4, Pother = 1, Pgroup = 8, Powner = 64, }; static char *sacfs = "fs.sac"; static uchar *data; static int blocksize; static Sac root; static Cache cache[CacheSize]; static ulong cacheage; static void sacdir(Chan *, SacDir*, char*); static ulong getl(void *p); static Sac *saccpy(Sac *s); static Sac *saclookup(Sac *s, char *name); static int sacdirread(Chan *, char *p, long off, long cnt); static void loadblock(void *buf, uchar *offset, int blocksize); static void sacfree(Sac*); static void pathtoqid(ulong path, Qid *q) { if(path & CHDIR) q->type = QTDIR; else q->type = QTFILE; q->path = path & ~(CHDIR|CHAPPEND|CHEXCL|CHMOUNT); q->vers = 0; } static void sacinit(void) { SacHeader *hdr; uchar *p; int i; p = (uchar*)Flash_tar+4; data = tarlookup(p, sacfs, &i); if(data == 0) { print("devsac: could not find file: %s\n", sacfs); return; } hdr = (SacHeader*)data; if(getl(hdr->magic) != Magic) { print("devsac: bad magic\n"); return; } blocksize = getl(hdr->blocksize); root.SacDir = *(SacDir*)(data + sizeof(SacHeader)); p = malloc(CacheSize*blocksize); if(p == nil) error("allocating cache"); for(i=0; iqid; c->dev = dev; c->aux = saccpy(&root); return c; } static Chan* sacclone(Chan *c, Chan *nc) { nc = devclone(c, nc); nc->aux = saccpy(c->aux); return nc; } static Walkqid* sacwalk(Chan *c, Chan *nc, char **name, int nname) { Sac *sac; int i, j, alloc; Walkqid *wq; char *n; Dir dir; if(nname > 0) isdir(c); alloc = 0; wq = smalloc(sizeof(Walkqid)+(nname-1)*sizeof(Qid)); if(waserror()){ if(alloc && wq->clone!=nil) cclose(wq->clone); free(wq); return nil; } if(nc == nil){ nc = devclone(c); nc->type = 0; /* device doesn't know about this channel yet */ alloc = 1; } wq->clone = nc; for(j=0; jaux; sac = saclookup(sac, name); if(sac == nil) { strncpy(up->error, Enonexist, NAMELEN); return 0; } nc->aux = sac; pathtoqid(getl(sac->qid), &nc->qid); } return 1; } static Chan* sacopen(Chan *c, int omode) { ulong t, mode; Sac *sac; static int access[] = { 0400, 0200, 0600, 0100 }; sac = c->aux; mode = getl(sac->mode); if(strcmp(up->user, sac->uid) == 0) mode = mode; else if(strcmp(up->user, sac->gid) == 0) mode = mode<<3; else mode = mode<<6; t = access[omode&3]; if((t & mode) != t) error(Eperm); c->offset = 0; c->mode = openmode(omode); c->flag |= COPEN; return c; } static long sacread(Chan *c, void *a, long n, vlong voff) { Sac *sac; char *buf, *buf2; int nn, cnt, i, j; uchar *blocks; long length; long off = voff; buf = a; cnt = n; if(c->qid.path & CHDIR){ cnt = (cnt/DIRLEN)*DIRLEN; if(off%DIRLEN) error("i/o error"); return sacdirread(c, buf, off, cnt); } sac = c->aux; length = getl(sac->length); if(off >= length) return 0; if(cnt > length-off) cnt = length-off; if(cnt == 0) return 0; n = cnt; blocks = data + getl(sac->blocks); buf2 = malloc(blocksize); while(cnt > 0) { i = off/blocksize; nn = blocksize; if(nn > length-i*blocksize) nn = length-i*blocksize; loadblock(buf2, blocks+i*OffsetSize, nn); j = off-i*blocksize; nn -= j; if(nn > cnt) nn = cnt; memmove(buf, buf2+j, nn); cnt -= nn; off += nn; buf += nn; } free(buf2); return n; } static long sacwrite(Chan *, void *, long, vlong) { error(Eperm); return 0; } static void sacclose(Chan* c) { Sac *sac = c->aux; c->aux = nil; sacfree(sac); } static void sacstat(Chan *c, char *db) { sacdir(c, c->aux, db); } static Sac* saccpy(Sac *s) { Sac *ss; ss = malloc(sizeof(Sac)); *ss = *s; if(ss->path) incref(ss->path); return ss; } static SacPath * sacpathalloc(SacPath *p, long blocks, int entry, int nentry) { SacPath *pp = malloc(sizeof(SacPath)); pp->ref = 1; pp->blocks = blocks; pp->entry = entry; pp->nentry = nentry; pp->up = p; return pp; } static void sacpathfree(SacPath *p) { if(p == nil) return; if(decref(p) > 0) return; sacpathfree(p->up); free(p); } static void sacfree(Sac *s) { sacpathfree(s->path); free(s); } static void sacdir(Chan *c, SacDir *s, char *buf) { Dir dir; memmove(dir.name, s->name, NAMELEN); dir.qid = (Qid){getl(s->qid), 0}; dir.mode = getl(s->mode); dir.length = getl(s->length); if(dir.mode &CHDIR) dir.length *= DIRLEN; memmove(dir.uid, s->uid, NAMELEN); memmove(dir.gid, s->gid, NAMELEN); dir.atime = getl(s->atime); dir.mtime = getl(s->mtime); dir.type = devtab[c->type]->dc; dir.dev = c->dev; convD2M(&dir, buf); } static void loadblock(void *buf, uchar *offset, int blocksize) { long block, n; ulong age; int i, j; block = getl(offset); if(block < 0) { block = -block; cacheage++; // age has wraped if(cacheage == 0) { for(i=0; ipath; if(p == nil || p->up == nil) { sacpathfree(p); *s = root; return s; } p = p->up; blocks = data + p->blocks; per = blocksize/sizeof(SacDir); i = p->entry/per; n = per; if(n > p->nentry-i*per) n = p->nentry-i*per; buf = malloc(per*sizeof(SacDir)); loadblock(buf, blocks + i*OffsetSize, n*sizeof(SacDir)); s->SacDir = buf[p->entry-i*per]; free(buf); incref(p); sacpathfree(s->path); s->path = p; return s; } static int sacdirread(Chan *c, char *p, long off, long cnt) { uchar *blocks; SacDir *buf; int iblock, per, i, j, n, ndir; Sac *s; s = c->aux; blocks = data + getl(s->blocks); per = blocksize/sizeof(SacDir); ndir = getl(s->length); off /= DIRLEN; cnt /= DIRLEN; if(off >= ndir) return 0; if(cnt > ndir-off) cnt = ndir-off; iblock = -1; buf = malloc(per*sizeof(SacDir)); for(i=off; i ndir-j*per) n = ndir-j*per; loadblock(buf, blocks + j*OffsetSize, n*sizeof(SacDir)); iblock = j; } j *= per; sacdir(c, buf+i-j, p); p += DIRLEN; } free(buf); return cnt*DIRLEN; } static Sac* saclookup(Sac *s, char *name) { int ndir; int i, j, k, n, per; uchar *blocks; SacDir *buf; int iblock; SacDir *sd; if(strcmp(name, "..") == 0) return sacparent(s); blocks = data + getl(s->blocks); per = blocksize/sizeof(SacDir); ndir = getl(s->length); buf = malloc(per*sizeof(SacDir)); iblock = -1; // linear search for(i=0; i ndir-j*per) n = ndir-j*per; loadblock(buf, blocks + j*OffsetSize, n*sizeof(SacDir)); iblock = j; } j *= per; sd = buf+i-j; k = strcmp(name, sd->name); if(k == 0) { s->path = sacpathalloc(s->path, getl(s->blocks), i, ndir); s->SacDir = *sd; free(buf); return s; } } free(buf); return 0; } static ulong getl(void *p) { uchar *a = p; return (a[0]<<24) | (a[1]<<16) | (a[2]<<8) | a[3]; } Dev sacdevtab = { 'C', "sac", devreset, sacinit, sacattach, sacclone, sacwalk, sacstat, sacopen, devcreate, sacclose, sacread, devbread, sacwrite, devbwrite, devremove, devwstat, };