9 #define BADBNO ((u64int)~0ULL)
14 static int checkfsblk(Fsblk*);
15 static int checkcgblk(Cgblk*);
16 static Block *ffsblockread(Fsys*, u64int);
17 static int ffssync(Fsys*);
18 static void ffsclose(Fsys*);
20 static u64int ffsxfileblock(Fsys *fs, Nfs3Handle *h, u64int offset);
21 static Nfs3Status ffsroot(Fsys*, Nfs3Handle*);
22 static Nfs3Status ffsgetattr(Fsys*, SunAuthUnix *au, Nfs3Handle*, Nfs3Attr*);
23 static Nfs3Status ffslookup(Fsys*, SunAuthUnix *au, Nfs3Handle*, char*, Nfs3Handle*);
24 static Nfs3Status ffsreadfile(Fsys*, SunAuthUnix *au, Nfs3Handle*, u32int, u64int, uchar**, u32int*, u1int*);
25 static Nfs3Status ffsreadlink(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char **link);
26 static Nfs3Status ffsreaddir(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int, u64int, uchar**, u32int*, u1int*);
27 static Nfs3Status ffsaccess(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int want, u32int *got, Nfs3Attr *attr);
30 fsysopenffs(Disk *disk)
35 fsys = emalloc(sizeof(Fsys));
36 fs = emalloc(sizeof(Ffs));
40 fsys->_readblock = ffsblockread;
41 fsys->_sync = ffssync;
42 fsys->_root = ffsroot;
43 fsys->_getattr = ffsgetattr;
44 fsys->_access = ffsaccess;
45 fsys->_lookup = ffslookup;
46 fsys->_readfile = ffsreadfile;
47 fsys->_readlink = ffsreadlink;
48 fsys->_readdir = ffsreaddir;
49 fsys->fileblock = ffsxfileblock;
62 ffscylgrp(Ffs *fs, u32int i, Block **pb)
70 b = diskread(fs->disk, fs->blocksize, (u64int)fs->cg[i].cgblkno*fs->blocksize);
74 if(checkcgblk(cg) < 0){
75 fprint(2, "checkcgblk %d %lud: %r\n", i, (ulong)fs->cg[i].cgblkno);
87 int off[] = { SBOFF, SBOFF2, SBOFFPIGGY };
102 for(i=0; i<nelem(off); i++){
103 if((b = diskread(disk, SBSIZE, off[i])) == nil)
105 fsblk = (Fsblk*)b->data;
106 // fprint(2, "offset of magic: %ld\n", offsetof(Fsblk, magic));
107 if((fs->ufs = checkfsblk(fsblk)) > 0)
115 fs->blocksize = fsblk->blocksize;
116 fs->nblock = (fsblk->nfrag+fsblk->fragsperblock-1) / fsblk->fragsperblock;
117 fs->fragsize = fsblk->fragsize;
118 fs->fragspergroup = fsblk->fragspergroup;
119 fs->fragsperblock = fsblk->fragsperblock;
120 fs->inosperblock = fsblk->inosperblock;
121 fs->inospergroup = fsblk->inospergroup;
123 fs->nfrag = fsblk->nfrag;
124 fs->ndfrag = fsblk->ndfrag;
127 * fs->blockspergroup = (u64int)fsblk->_cylspergroup *
128 * fsblk->secspercyl * BYTESPERSEC / fsblk->blocksize;
129 * for UFS1, but this should work for both UFS1 and UFS2
131 fs->blockspergroup = (u64int)fsblk->fragspergroup / fsblk->fragsperblock;
132 fs->ncg = fsblk->ncg;
134 fsys->blocksize = fs->blocksize;
135 fsys->nblock = fs->nblock;
137 if(debug) fprint(2, "ffs %lld %d-byte blocks, %d cylinder groups\n",
138 fs->nblock, fs->blocksize, fs->ncg);
139 if(debug) fprint(2, "\tinospergroup %d perblock %d blockspergroup %lld\n",
140 fs->inospergroup, fs->inosperblock, fs->blockspergroup);
143 fs->cg = emalloc(fs->ncg*sizeof(Cylgrp));
144 for(i=0; i<fs->ncg; i++){
147 cg->bno = (u64int)fs->blockspergroup*i;
149 cg->bno = fs->blockspergroup*i + fsblk->_cgoffset * (i & ~fsblk->_cgmask);
150 cg->cgblkno = cg->bno + fsblk->cfragno/fs->fragsperblock;
151 cg->ibno = cg->bno + fsblk->ifragno/fs->fragsperblock;
152 cg->dbno = cg->bno + fsblk->dfragno/fs->fragsperblock;
155 if((cgb = diskread(disk, fs->blocksize, (u64int)cg->cgblkno*fs->blocksize)) == nil)
158 cgblk = (Cgblk*)cgb->data;
159 if(checkcgblk(cgblk) < 0){
163 if(cgblk->nfrag % fs->fragsperblock && i != fs->ncg-1){
164 werrstr("fractional number of blocks in non-last cylinder group %d", cgblk->nfrag);
168 /* cg->nfrag = cgblk->nfrag; */
169 /* cg->nblock = (cgblk->nfrag+fs->fragsperblock-1) / fs->fragsperblock; */
170 /* fprint(2, "cg #%d: cgblk %lud, %d blocks, %d inodes\n", cgblk->num, (ulong)cg->cgblkno, cg->nblock, cg->nino); */
194 checkfsblk(Fsblk *super)
196 // fprint(2, "ffs magic 0x%ux\n", super->magic);
197 if(super->magic == FSMAGIC){
198 super->time = super->_time;
199 super->nfrag = super->_nfrag;
200 super->ndfrag = super->_ndfrag;
201 super->flags = super->_flags;
204 if(super->magic == FSMAGIC2){
208 werrstr("bad super block");
213 checkcgblk(Cgblk *cg)
215 if(cg->magic != CGMAGIC){
216 werrstr("bad cylinder group block");
223 * Read block #bno from the disk, zeroing unused data.
224 * If there is no data whatsoever, it's okay to return nil.
228 ffsblockread(Fsys *fsys, u64int bno)
232 int frag, fsize, avail;
238 i = bno / fs->blockspergroup;
239 o = bno % fs->blockspergroup;
243 if((cgblk = ffscylgrp(fs, i, &b)) == nil)
246 fmap = (u8int*)cgblk+cgblk->fmapoff;
247 frag = fs->fragsperblock;
250 sysfatal("bad frag");
255 avail = (fmap[o>>1] >> ((o&1)*4)) & 0xF;
258 avail = (fmap[o>>2] >> ((o&3)*2)) & 0x3;
261 avail = (fmap[o>>3] >> (o&7)) & 0x1;
266 if(avail == ((1<<frag)-1))
271 if((b = diskread(fs->disk, fs->blocksize, bno*fs->blocksize)) == nil){
272 fprint(2, "diskread failed!!!\n");
276 fsize = fs->fragsize;
277 for(i=0; i<frag; i++)
279 memset(b->data + fsize*i, 0, fsize);
284 ffsdatablock(Ffs *fs, u64int bno, int size)
294 if(fsize < fs->fragsize)
295 fsize = fs->fragsize;
297 if(bno >= fs->nfrag){
298 fprint(2, "ffs: request for block %#lux; nfrag %#llux\n", (ulong)bno, fs->nfrag);
301 diskaddr = (u64int)bno*fs->fragsize;
302 b = diskread(fs->disk, fsize, diskaddr);
304 fprint(2, "ffs: disk i/o at %#llux for %#ux: %r\n", diskaddr, fsize);
308 fprint(2, "ffs: disk i/o at %#llux for %#ux got %#ux\n", diskaddr, fsize,
318 ifetch(Ffs *fs, u64int bno, u32int off)
324 b = ffsdatablock(fs, bno, fs->blocksize);
328 bno = ((u64int*)b->data)[off];
330 bno = ((u32int*)b->data)[off];
336 ffsfileblockno(Ffs *fs, Inode *ino, u64int bno)
341 if(debug) fprint(2, "ffsfileblock %lud: direct %#lux\n", (ulong)bno, (ulong)ino->db[bno]);
345 ppb = fs->blocksize/4;
347 if(bno < ppb) /* single indirect */
348 return ifetch(fs, ino->ib[0], bno);
352 return ifetch(fs, ifetch(fs, ino->ib[1], bno/ppb), bno%ppb);
355 if(bno/ppb/ppb/ppb == 0) /* bno < ppb*ppb*ppb w/o overflow */
356 return ifetch(fs, ifetch(fs, ifetch(fs, ino->ib[2], bno/ppb/ppb), (bno/ppb)%ppb), bno%ppb);
358 fprint(2, "ffsfileblock %llud: way too big\n", bno+NDADDR+ppb+ppb*ppb);
363 ffsfileblock(Ffs *fs, Inode *ino, u64int bno, int size)
367 b = ffsfileblockno(fs, ino, bno);
370 return ffsdatablock(fs, b, size);
374 * NFS handles are 4-byte inode number.
377 mkhandle(Nfs3Handle *h, u64int ino)
389 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
392 static u64int lastiaddr; /* debugging */
395 inode1to2(Inode1 *i1, Inode *i2)
399 memset(i2, 0, sizeof *i2);
401 i2->nlink = i1->nlink;
403 i2->atime = i1->atime;
404 i2->atimensec = i1->atimensec;
405 i2->mtime = i1->mtime;
406 i2->mtimensec = i1->mtimensec;
407 i2->ctime = i1->ctime;
408 i2->ctimensec = i1->ctimensec;
409 for(i=0; i<NDADDR; i++)
410 i2->db[i] = i1->db[i];
411 for(i=0; i<NIADDR; i++)
412 i2->ib[i] = i1->ib[i];
413 i2->flags = i1->flags;
414 i2->nblock = i1->nblock;
421 handle2ino(Ffs *fs, Nfs3Handle *h, u32int *pinum, Inode *ino)
432 return Nfs3ErrBadHandle;
433 inum = byte2u32(h->h);
436 if(debug) print("inum %d...", (int)inum);
438 /* fetch inode from disk */
439 i = inum / fs->inospergroup;
440 ioff = inum % fs->inospergroup;
441 if(debug)print("cg %d off %d...", i, (int)ioff);
443 return Nfs3ErrBadHandle;
446 if(debug) print("cg->ibno %lld ufs %d...", cg->ibno, fs->ufs);
447 iaddr = (cg->ibno+ioff/fs->inosperblock)*(vlong)fs->blocksize;
448 ioff = ioff%fs->inosperblock;
449 if((b = diskread(fs->disk, fs->blocksize, iaddr)) == nil)
452 *ino = ((Inode*)b->data)[ioff];
453 lastiaddr = iaddr+ioff*sizeof(Inode);
455 ino1 = ((Inode1*)b->data)[ioff];
456 inode1to2(&ino1, ino);
457 lastiaddr = iaddr+ioff*sizeof(Inode1);
464 ffsroot(Fsys *fsys, Nfs3Handle *h)
472 ino2attr(Ffs *fs, Inode *ino, u32int inum, Nfs3Attr *attr)
477 switch(ino->mode&IFMT){
479 attr->type = Nfs3FileFifo;
482 attr->type = Nfs3FileChar;
485 attr->type = Nfs3FileDir;
488 attr->type = Nfs3FileBlock;
491 attr->type = Nfs3FileReg;
494 attr->type = Nfs3FileSymlink;
497 attr->type = Nfs3FileSocket;
501 return Nfs3ErrBadHandle;
504 attr->mode = ino->mode&07777;
505 attr->nlink = ino->nlink;
506 attr->uid = ino->uid;
507 attr->gid = ino->gid;
508 attr->size = ino->size;
509 attr->used = ino->nblock*fs->blocksize;
510 if(attr->type==Nfs3FileBlock || attr->type==Nfs3FileChar){
512 attr->major = (rdev>>8)&0xFF;
513 attr->minor = rdev & 0xFFFF00FF;
520 attr->atime.sec = ino->atime;
521 attr->atime.nsec = ino->atimensec;
522 attr->mtime.sec = ino->mtime;
523 attr->mtime.nsec = ino->mtimensec;
524 attr->ctime.sec = ino->ctime;
525 attr->ctime.nsec = ino->ctimensec;
530 ingroup(SunAuthUnix *au, uint gid)
534 for(i=0; i<au->ng; i++)
541 inoperm(Inode *ino, SunAuthUnix *au, int need)
548 have = ino->mode&0777;
549 if(ino->uid == au->uid)
551 else if(ino->gid == au->gid || ingroup(au, ino->gid))
554 if((have&need) != need)
555 return Nfs3ErrNotOwner; /* really EPERM */
560 ffsgetattr(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, Nfs3Attr *attr)
568 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
571 USED(au); /* anyone can getattr */
573 return ino2attr(fs, &ino, inum, attr);
577 ffsaccess(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int want, u32int *got, Nfs3Attr *attr)
586 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
589 have = ino.mode&0777;
590 if(ino.uid == au->uid)
592 else if(ino.gid == au->gid || ingroup(au, ino.gid))
596 if((want&Nfs3AccessRead) && (have&AREAD))
597 *got |= Nfs3AccessRead;
598 if((want&Nfs3AccessLookup) && (ino.mode&IFMT)==IFDIR && (have&AEXEC))
599 *got |= Nfs3AccessLookup;
600 if((want&Nfs3AccessExecute) && (ino.mode&IFMT)!=IFDIR && (have&AEXEC))
601 *got |= Nfs3AccessExecute;
603 return ino2attr(fs, &ino, inum, attr);
607 ffslookup(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char *name, Nfs3Handle *nh)
620 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
623 if((ino.mode&IFMT) != IFDIR)
624 return Nfs3ErrNotDir;
626 if((ok = inoperm(&ino, au, AEXEC)) != Nfs3Ok)
630 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
631 for(i=0; i<nblock; i++){
633 want = ino.size % fs->blocksize;
635 want = fs->blocksize;
636 b = ffsfileblock(fs, &ino, i, want);
645 fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
651 fprint(2, "bad len %d at offset %d of %d\n", de->reclen, (int)(p-b->data), b->len);
656 if(4+2+2+de->namlen > de->reclen){
658 fprint(2, "bad namelen %d at offset %d of %d\n", de->namlen, (int)(p-b->data), b->len);
661 if(de->namlen == len && memcmp(de->name, name, len) == 0){
662 mkhandle(nh, de->ino);
673 ffsreaddir(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count, u64int cookie, uchar **pdata, u32int *pcount, u1int *peof)
678 uchar *data, *dp, *dep, *p, *ep, *ndp;
688 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
691 if((ino.mode&IFMT) != IFDIR)
692 return Nfs3ErrNotDir;
694 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
697 if(cookie >= ino.size){
709 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
710 i = cookie/fs->blocksize;
711 off = cookie%fs->blocksize;
713 for(; i<nblock && !done; i++){
715 want = ino.size % fs->blocksize;
717 want = fs->blocksize;
718 b = ffsfileblock(fs, &ino, i, want);
723 memset(&e, 0, sizeof e);
727 if(debug) fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
732 if(debug) fprint(2, "reclen %d at offset %d of %d\n", de->reclen, (int)(p-b->data), b->len);
736 if(debug) fprint(2, "zero inode\n");
739 if(4+2+2+de->namlen > de->reclen){
740 if(debug) fprint(2, "bad namlen %d reclen %d at offset %d of %d\n", de->namlen, de->reclen, (int)(p-b->data), b->len);
743 if(de->name[de->namlen] != 0){
744 if(debug) fprint(2, "bad name %d %.*s\n", de->namlen, de->namlen, de->name);
747 if(debug) print("%s/%d ", de->name, (int)de->ino);
748 if((uchar*)de - b->data < off)
752 e.namelen = de->namlen;
753 e.cookie = (u64int)i*fs->blocksize + (p - b->data);
754 if(nfs3entrypack(dp, dep, &ndp, &e) < 0){
772 ffsxfileblock(Fsys *fsys, Nfs3Handle *h, u64int offset)
780 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok){
784 if(offset == 1) /* clumsy hack for debugging */
786 if(offset >= ino.size){
787 werrstr("beyond end of file");
790 bno = offset/fs->blocksize;
791 bno = ffsfileblockno(fs, &ino, bno);
794 return bno*(u64int)fs->fragsize;
798 ffsreadfile(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count,
799 u64int offset, uchar **pdata, u32int *pcount, u1int *peof)
804 int off, want, fragcount;
809 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
812 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
815 if(offset >= ino.size){
821 if(offset+count > ino.size)
822 count = ino.size-offset;
823 if(offset/fs->blocksize != (offset+count-1)/fs->blocksize)
824 count = fs->blocksize - offset%fs->blocksize;
826 data = malloc(count);
830 want = offset%fs->blocksize+count;
831 if(want%fs->fragsize)
832 want += fs->fragsize - want%fs->fragsize;
834 b = ffsfileblock(fs, &ino, offset/fs->blocksize, want);
836 /* BUG: distinguish sparse file from I/O error */
837 memset(data, 0, count);
839 off = offset%fs->blocksize;
840 fragcount = count; /* need signed variable */
841 if(off+fragcount > b->len){
842 fragcount = b->len - off;
847 memmove(data, b->data+off, fragcount);
851 *peof = (offset+count == ino.size);
858 ffsreadlink(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char **link)
867 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
869 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
877 /* assumes symlink fits in one block */
878 b = ffsfileblock(fs, &ino, 0, len);
881 if(memchr(b->data, 0, len) != nil){
885 *link = malloc(len+1);
890 memmove(*link, b->data, len);
896 if(len > sizeof ino.db + sizeof ino.ib)
899 *link = malloc(len+1);
902 memmove(*link, ino.db, ino.size);