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)
114 fs->blocksize = fsblk->blocksize;
115 fs->nblock = (fsblk->nfrag+fsblk->fragsperblock-1) / fsblk->fragsperblock;
116 fs->fragsize = fsblk->fragsize;
117 fs->fragspergroup = fsblk->fragspergroup;
118 fs->fragsperblock = fsblk->fragsperblock;
119 fs->inosperblock = fsblk->inosperblock;
120 fs->inospergroup = fsblk->inospergroup;
122 fs->nfrag = fsblk->nfrag;
123 fs->ndfrag = fsblk->ndfrag;
126 * fs->blockspergroup = (u64int)fsblk->_cylspergroup *
127 * fsblk->secspercyl * BYTESPERSEC / fsblk->blocksize;
128 * for UFS1, but this should work for both UFS1 and UFS2
130 fs->blockspergroup = (u64int)fsblk->fragspergroup / fsblk->fragsperblock;
131 fs->ncg = fsblk->ncg;
133 fsys->blocksize = fs->blocksize;
134 fsys->nblock = fs->nblock;
136 if(debug) fprint(2, "ffs %lld %d-byte blocks, %d cylinder groups\n",
137 fs->nblock, fs->blocksize, fs->ncg);
138 if(debug) fprint(2, "\tinospergroup %d perblock %d blockspergroup %lld\n",
139 fs->inospergroup, fs->inosperblock, fs->blockspergroup);
142 fs->cg = emalloc(fs->ncg*sizeof(Cylgrp));
143 for(i=0; i<fs->ncg; i++){
146 cg->bno = (u64int)fs->blockspergroup*i;
148 cg->bno = fs->blockspergroup*i + fsblk->_cgoffset * (i & ~fsblk->_cgmask);
149 cg->cgblkno = cg->bno + fsblk->cfragno/fs->fragsperblock;
150 cg->ibno = cg->bno + fsblk->ifragno/fs->fragsperblock;
151 cg->dbno = cg->bno + fsblk->dfragno/fs->fragsperblock;
154 if((cgb = diskread(disk, fs->blocksize, (u64int)cg->cgblkno*fs->blocksize)) == nil)
157 cgblk = (Cgblk*)cgb->data;
158 if(checkcgblk(cgblk) < 0){
162 if(cgblk->nfrag % fs->fragsperblock && i != fs->ncg-1){
163 werrstr("fractional number of blocks in non-last cylinder group %d", cgblk->nfrag);
167 // cg->nfrag = cgblk->nfrag;
168 // cg->nblock = (cgblk->nfrag+fs->fragsperblock-1) / fs->fragsperblock;
169 // fprint(2, "cg #%d: cgblk %lud, %d blocks, %d inodes\n", cgblk->num, (ulong)cg->cgblkno, cg->nblock, cg->nino);
193 checkfsblk(Fsblk *super)
195 fprint(2, "ffs magic 0x%ux\n", super->magic);
196 if(super->magic == FSMAGIC){
197 super->time = super->_time;
198 super->nfrag = super->_nfrag;
199 super->ndfrag = super->_ndfrag;
200 super->flags = super->_flags;
203 if(super->magic == FSMAGIC2){
207 werrstr("bad super block");
212 checkcgblk(Cgblk *cg)
214 if(cg->magic != CGMAGIC){
215 werrstr("bad cylinder group block");
222 * Read block #bno from the disk, zeroing unused data.
223 * If there is no data whatsoever, it's okay to return nil.
227 ffsblockread(Fsys *fsys, u64int bno)
231 int frag, fsize, avail;
237 i = bno / fs->blockspergroup;
238 o = bno % fs->blockspergroup;
242 if((cgblk = ffscylgrp(fs, i, &b)) == nil)
245 fmap = (u8int*)cgblk+cgblk->fmapoff;
246 frag = fs->fragsperblock;
249 sysfatal("bad frag");
254 avail = (fmap[o>>1] >> ((o&1)*4)) & 0xF;
257 avail = (fmap[o>>2] >> ((o&3)*2)) & 0x3;
260 avail = (fmap[o>>3] >> (o&7)) & 0x1;
265 if(avail == ((1<<frag)-1))
270 if((b = diskread(fs->disk, fs->blocksize, bno*fs->blocksize)) == nil){
271 fprint(2, "diskread failed!!!\n");
275 fsize = fs->fragsize;
276 for(i=0; i<frag; i++)
278 memset(b->data + fsize*i, 0, fsize);
283 ffsdatablock(Ffs *fs, u64int bno, int size)
293 if(fsize < fs->fragsize)
294 fsize = fs->fragsize;
296 if(bno >= fs->nfrag){
297 fprint(2, "ffs: request for block %#lux; nfrag %#llux\n", (ulong)bno, fs->nfrag);
300 diskaddr = (u64int)bno*fs->fragsize;
301 b = diskread(fs->disk, fsize, diskaddr);
303 fprint(2, "ffs: disk i/o at %#llux for %#ux: %r\n", diskaddr, fsize);
307 fprint(2, "ffs: disk i/o at %#llux for %#ux got %#ux\n", diskaddr, fsize,
317 ifetch(Ffs *fs, u64int bno, u32int off)
323 b = ffsdatablock(fs, bno, fs->blocksize);
327 bno = ((u64int*)b->data)[off];
329 bno = ((u32int*)b->data)[off];
335 ffsfileblockno(Ffs *fs, Inode *ino, u64int bno)
340 if(debug) fprint(2, "ffsfileblock %lud: direct %#lux\n", (ulong)bno, (ulong)ino->db[bno]);
344 ppb = fs->blocksize/4;
346 if(bno < ppb) /* single indirect */
347 return ifetch(fs, ino->ib[0], bno);
351 return ifetch(fs, ifetch(fs, ino->ib[1], bno/ppb), bno%ppb);
354 if(bno/ppb/ppb/ppb == 0) /* bno < ppb*ppb*ppb w/o overflow */
355 return ifetch(fs, ifetch(fs, ifetch(fs, ino->ib[2], bno/ppb/ppb), (bno/ppb)%ppb), bno%ppb);
357 fprint(2, "ffsfileblock %llud: way too big\n", bno+NDADDR+ppb+ppb*ppb);
362 ffsfileblock(Ffs *fs, Inode *ino, u64int bno, int size)
366 b = ffsfileblockno(fs, ino, bno);
369 return ffsdatablock(fs, b, size);
373 * NFS handles are 4-byte inode number.
376 mkhandle(Nfs3Handle *h, u64int ino)
388 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
391 static u64int lastiaddr; /* debugging */
394 inode1to2(Inode1 *i1, Inode *i2)
398 memset(i2, 0, sizeof *i2);
400 i2->nlink = i1->nlink;
402 i2->atime = i1->atime;
403 i2->atimensec = i1->atimensec;
404 i2->mtime = i1->mtime;
405 i2->mtimensec = i1->mtimensec;
406 i2->ctime = i1->ctime;
407 i2->ctimensec = i1->ctimensec;
408 for(i=0; i<NDADDR; i++)
409 i2->db[i] = i1->db[i];
410 for(i=0; i<NIADDR; i++)
411 i2->ib[i] = i1->ib[i];
412 i2->flags = i1->flags;
413 i2->nblock = i1->nblock;
420 handle2ino(Ffs *fs, Nfs3Handle *h, u32int *pinum, Inode *ino)
431 return Nfs3ErrBadHandle;
432 inum = byte2u32(h->h);
435 if(debug) print("inum %d...", (int)inum);
437 /* fetch inode from disk */
438 i = inum / fs->inospergroup;
439 ioff = inum % fs->inospergroup;
440 if(debug)print("cg %d off %d...", i, (int)ioff);
442 return Nfs3ErrBadHandle;
445 if(debug) print("cg->ibno %lld ufs %d...", cg->ibno, fs->ufs);
446 iaddr = (cg->ibno+ioff/fs->inosperblock)*(vlong)fs->blocksize;
447 ioff = ioff%fs->inosperblock;
448 if((b = diskread(fs->disk, fs->blocksize, iaddr)) == nil)
451 *ino = ((Inode*)b->data)[ioff];
452 lastiaddr = iaddr+ioff*sizeof(Inode);
454 ino1 = ((Inode1*)b->data)[ioff];
455 inode1to2(&ino1, ino);
456 lastiaddr = iaddr+ioff*sizeof(Inode1);
463 ffsroot(Fsys *fsys, Nfs3Handle *h)
471 ino2attr(Ffs *fs, Inode *ino, u32int inum, Nfs3Attr *attr)
476 switch(ino->mode&IFMT){
478 attr->type = Nfs3FileFifo;
481 attr->type = Nfs3FileChar;
484 attr->type = Nfs3FileDir;
487 attr->type = Nfs3FileBlock;
490 attr->type = Nfs3FileReg;
493 attr->type = Nfs3FileSymlink;
496 attr->type = Nfs3FileSocket;
500 return Nfs3ErrBadHandle;
503 attr->mode = ino->mode&07777;
504 attr->nlink = ino->nlink;
505 attr->uid = ino->uid;
506 attr->gid = ino->gid;
507 attr->size = ino->size;
508 attr->used = ino->nblock*fs->blocksize;
509 if(attr->type==Nfs3FileBlock || attr->type==Nfs3FileChar){
511 attr->major = (rdev>>8)&0xFF;
512 attr->minor = rdev & 0xFFFF00FF;
519 attr->atime.sec = ino->atime;
520 attr->atime.nsec = ino->atimensec;
521 attr->mtime.sec = ino->mtime;
522 attr->mtime.nsec = ino->mtimensec;
523 attr->ctime.sec = ino->ctime;
524 attr->ctime.nsec = ino->ctimensec;
529 ingroup(SunAuthUnix *au, uint gid)
533 for(i=0; i<au->ng; i++)
540 inoperm(Inode *ino, SunAuthUnix *au, int need)
547 have = ino->mode&0777;
548 if(ino->uid == au->uid)
550 else if(ino->gid == au->gid || ingroup(au, ino->gid))
553 if((have&need) != need)
554 return Nfs3ErrNotOwner; /* really EPERM */
559 ffsgetattr(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, Nfs3Attr *attr)
567 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
570 USED(au); /* anyone can getattr */
572 return ino2attr(fs, &ino, inum, attr);
576 ffsaccess(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int want, u32int *got, Nfs3Attr *attr)
585 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
588 have = ino.mode&0777;
589 if(ino.uid == au->uid)
591 else if(ino.gid == au->gid || ingroup(au, ino.gid))
595 if((want&Nfs3AccessRead) && (have&AREAD))
596 *got |= Nfs3AccessRead;
597 if((want&Nfs3AccessLookup) && (ino.mode&IFMT)==IFDIR && (have&AEXEC))
598 *got |= Nfs3AccessLookup;
599 if((want&Nfs3AccessExecute) && (ino.mode&IFMT)!=IFDIR && (have&AEXEC))
600 *got |= Nfs3AccessExecute;
602 return ino2attr(fs, &ino, inum, attr);
606 ffslookup(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char *name, Nfs3Handle *nh)
619 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
622 if((ino.mode&IFMT) != IFDIR)
623 return Nfs3ErrNotDir;
625 if((ok = inoperm(&ino, au, AEXEC)) != Nfs3Ok)
629 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
630 for(i=0; i<nblock; i++){
632 want = ino.size % fs->blocksize;
634 want = fs->blocksize;
635 b = ffsfileblock(fs, &ino, i, want);
644 fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
650 fprint(2, "bad len %d at offset %d of %d\n", de->reclen, (int)(p-b->data), b->len);
655 if(4+2+2+de->namlen > de->reclen){
657 fprint(2, "bad namelen %d at offset %d of %d\n", de->namlen, (int)(p-b->data), b->len);
660 if(de->namlen == len && memcmp(de->name, name, len) == 0){
661 mkhandle(nh, de->ino);
672 ffsreaddir(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count, u64int cookie, uchar **pdata, u32int *pcount, u1int *peof)
677 uchar *data, *dp, *dep, *p, *ep, *ndp;
687 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
690 if((ino.mode&IFMT) != IFDIR)
691 return Nfs3ErrNotDir;
693 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
696 if(cookie >= ino.size){
708 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
709 i = cookie/fs->blocksize;
710 off = cookie%fs->blocksize;
712 for(; i<nblock && !done; i++){
714 want = ino.size % fs->blocksize;
716 want = fs->blocksize;
717 b = ffsfileblock(fs, &ino, i, want);
722 memset(&e, 0, sizeof e);
726 if(debug) fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
731 if(debug) fprint(2, "reclen %d at offset %d of %d\n", de->reclen, (int)(p-b->data), b->len);
735 if(debug) fprint(2, "zero inode\n");
738 if(4+2+2+de->namlen > de->reclen){
739 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);
742 if(de->name[de->namlen] != 0){
743 if(debug) fprint(2, "bad name %d %.*s\n", de->namlen, de->namlen, de->name);
746 if(debug) print("%s/%d ", de->name, (int)de->ino);
747 if((uchar*)de - b->data < off)
751 e.cookie = (u64int)i*fs->blocksize + (p - b->data);
752 if(nfs3entrypack(dp, dep, &ndp, &e) < 0){
770 ffsxfileblock(Fsys *fsys, Nfs3Handle *h, u64int offset)
778 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok){
782 if(offset == 1) /* clumsy hack for debugging */
784 if(offset >= ino.size){
785 werrstr("beyond end of file");
788 bno = offset/fs->blocksize;
789 bno = ffsfileblockno(fs, &ino, bno);
792 return bno*(u64int)fs->fragsize;
796 ffsreadfile(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count,
797 u64int offset, uchar **pdata, u32int *pcount, u1int *peof)
802 int off, want, fragcount;
807 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
810 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
813 if(offset >= ino.size){
819 if(offset+count > ino.size)
820 count = ino.size-offset;
821 if(offset/fs->blocksize != (offset+count-1)/fs->blocksize)
822 count = fs->blocksize - offset%fs->blocksize;
824 data = malloc(count);
828 want = offset%fs->blocksize+count;
829 if(want%fs->fragsize)
830 want += fs->fragsize - want%fs->fragsize;
832 b = ffsfileblock(fs, &ino, offset/fs->blocksize, want);
834 /* BUG: distinguish sparse file from I/O error */
835 memset(data, 0, count);
837 off = offset%fs->blocksize;
838 fragcount = count; /* need signed variable */
839 if(off+fragcount > b->len){
840 fragcount = b->len - off;
845 memmove(data, b->data+off, fragcount);
849 *peof = (offset+count == ino.size);
856 ffsreadlink(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char **link)
865 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
867 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
875 /* assumes symlink fits in one block */
876 b = ffsfileblock(fs, &ino, 0, len);
879 if(memchr(b->data, 0, len) != nil){
883 *link = malloc(len+1);
888 memmove(*link, b->data, len);
894 if(len > sizeof ino.db + sizeof ino.ib)
897 *link = malloc(len+1);
900 memmove(*link, ino.db, ino.size);