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 };
101 for(i=0; i<nelem(off); i++){
102 if((b = diskread(disk, SBSIZE, off[i])) == nil)
104 fsblk = (Fsblk*)b->data;
105 fprint(2, "offset of magic: %d\n", offsetof(Fsblk, magic));
106 if((fs->ufs = checkfsblk(fsblk)) > 0)
113 fs->blocksize = fsblk->blocksize;
114 fs->nblock = (fsblk->nfrag+fsblk->fragsperblock-1) / fsblk->fragsperblock;
115 fs->fragsize = fsblk->fragsize;
116 fs->fragspergroup = fsblk->fragspergroup;
117 fs->fragsperblock = fsblk->fragsperblock;
118 fs->inosperblock = fsblk->inosperblock;
119 fs->inospergroup = fsblk->inospergroup;
121 fs->nfrag = fsblk->nfrag;
122 fs->ndfrag = fsblk->ndfrag;
125 * fs->blockspergroup = (u64int)fsblk->_cylspergroup *
126 * fsblk->secspercyl * BYTESPERSEC / fsblk->blocksize;
127 * for UFS1, but this should work for both UFS1 and UFS2
129 fs->blockspergroup = (u64int)fsblk->fragspergroup / fsblk->fragsperblock;
130 fs->ncg = fsblk->ncg;
132 fsys->blocksize = fs->blocksize;
133 fsys->nblock = fs->nblock;
135 if(debug) fprint(2, "ffs %lld %d-byte blocks, %d cylinder groups\n",
136 fs->nblock, fs->blocksize, fs->ncg);
137 if(debug) fprint(2, "\tinospergroup %d perblock %d blockspergroup %lld\n",
138 fs->inospergroup, fs->inosperblock, fs->blockspergroup);
141 fs->cg = emalloc(fs->ncg*sizeof(Cylgrp));
142 for(i=0; i<fs->ncg; i++){
145 cg->bno = (u64int)fs->blockspergroup*i;
147 cg->bno = fs->blockspergroup*i + fsblk->_cgoffset * (i & ~fsblk->_cgmask);
148 cg->cgblkno = cg->bno + fsblk->cfragno/fs->fragsperblock;
149 cg->ibno = cg->bno + fsblk->ifragno/fs->fragsperblock;
150 cg->dbno = cg->bno + fsblk->dfragno/fs->fragsperblock;
153 if((cgb = diskread(disk, fs->blocksize, (u64int)cg->cgblkno*fs->blocksize)) == nil)
156 cgblk = (Cgblk*)cgb->data;
157 if(checkcgblk(cgblk) < 0){
161 if(cgblk->nfrag % fs->fragsperblock && i != fs->ncg-1){
162 werrstr("fractional number of blocks in non-last cylinder group %d", cgblk->nfrag);
166 // cg->nfrag = cgblk->nfrag;
167 // cg->nblock = (cgblk->nfrag+fs->fragsperblock-1) / fs->fragsperblock;
168 // fprint(2, "cg #%d: cgblk %lud, %d blocks, %d inodes\n", cgblk->num, (ulong)cg->cgblkno, cg->nblock, cg->nino);
192 checkfsblk(Fsblk *super)
194 fprint(2, "ffs magic 0x%ux\n", super->magic);
195 if(super->magic == FSMAGIC){
196 super->time = super->_time;
197 super->nfrag = super->_nfrag;
198 super->ndfrag = super->_ndfrag;
199 super->flags = super->_flags;
202 if(super->magic == FSMAGIC2){
206 werrstr("bad super block");
211 checkcgblk(Cgblk *cg)
213 if(cg->magic != CGMAGIC){
214 werrstr("bad cylinder group block");
221 * Read block #bno from the disk, zeroing unused data.
222 * If there is no data whatsoever, it's okay to return nil.
226 ffsblockread(Fsys *fsys, u64int bno)
230 int frag, fsize, avail;
236 i = bno / fs->blockspergroup;
237 o = bno % fs->blockspergroup;
241 if((cgblk = ffscylgrp(fs, i, &b)) == nil)
244 fmap = (u8int*)cgblk+cgblk->fmapoff;
245 frag = fs->fragsperblock;
248 sysfatal("bad frag");
253 avail = (fmap[o>>1] >> ((o&1)*4)) & 0xF;
256 avail = (fmap[o>>2] >> ((o&3)*2)) & 0x3;
259 avail = (fmap[o>>3] >> (o&7)) & 0x1;
264 if(avail == ((1<<frag)-1))
269 if((b = diskread(fs->disk, fs->blocksize, bno*fs->blocksize)) == nil){
270 fprint(2, "diskread failed!!!\n");
274 fsize = fs->fragsize;
275 for(i=0; i<frag; i++)
277 memset(b->data + fsize*i, 0, fsize);
282 ffsdatablock(Ffs *fs, u64int bno, int size)
292 if(fsize < fs->fragsize)
293 fsize = fs->fragsize;
295 if(bno >= fs->nfrag){
296 fprint(2, "ffs: request for block %#lux; nfrag %#x\n", (ulong)bno, fs->nfrag);
299 diskaddr = (u64int)bno*fs->fragsize;
300 b = diskread(fs->disk, fsize, diskaddr);
302 fprint(2, "ffs: disk i/o at %#llux for %#ux: %r\n", diskaddr, fsize);
306 fprint(2, "ffs: disk i/o at %#llux for %#ux got %#ux\n", diskaddr, fsize,
316 ifetch(Ffs *fs, u64int bno, u32int off)
322 b = ffsdatablock(fs, bno, fs->blocksize);
326 bno = ((u64int*)b->data)[off];
328 bno = ((u32int*)b->data)[off];
334 ffsfileblockno(Ffs *fs, Inode *ino, u64int bno)
339 if(debug) fprint(2, "ffsfileblock %lud: direct %#lux\n", (ulong)bno, (ulong)ino->db[bno]);
343 ppb = fs->blocksize/4;
345 if(bno < ppb) /* single indirect */
346 return ifetch(fs, ino->ib[0], bno);
350 return ifetch(fs, ifetch(fs, ino->ib[1], bno/ppb), bno%ppb);
353 if(bno/ppb/ppb/ppb == 0) /* bno < ppb*ppb*ppb w/o overflow */
354 return ifetch(fs, ifetch(fs, ifetch(fs, ino->ib[2], bno/ppb/ppb), (bno/ppb)%ppb), bno%ppb);
356 fprint(2, "ffsfileblock %llud: way too big\n", bno+NDADDR+ppb+ppb*ppb);
361 ffsfileblock(Ffs *fs, Inode *ino, u64int bno, int size)
365 b = ffsfileblockno(fs, ino, bno);
368 return ffsdatablock(fs, b, size);
372 * NFS handles are 4-byte inode number.
375 mkhandle(Nfs3Handle *h, u64int ino)
387 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
390 static u64int lastiaddr; /* debugging */
393 inode1to2(Inode1 *i1, Inode *i2)
397 memset(i2, 0, sizeof *i2);
399 i2->nlink = i1->nlink;
401 i2->atime = i1->atime;
402 i2->atimensec = i1->atimensec;
403 i2->mtime = i1->mtime;
404 i2->mtimensec = i1->mtimensec;
405 i2->ctime = i1->ctime;
406 i2->ctimensec = i1->ctimensec;
407 for(i=0; i<NDADDR; i++)
408 i2->db[i] = i1->db[i];
409 for(i=0; i<NIADDR; i++)
410 i2->ib[i] = i1->ib[i];
411 i2->flags = i1->flags;
412 i2->nblock = i1->nblock;
419 handle2ino(Ffs *fs, Nfs3Handle *h, u32int *pinum, Inode *ino)
430 return Nfs3ErrBadHandle;
431 inum = byte2u32(h->h);
434 if(debug) print("inum %d...", (int)inum);
436 /* fetch inode from disk */
437 i = inum / fs->inospergroup;
438 ioff = inum % fs->inospergroup;
439 if(debug)print("cg %d off %d...", i, (int)ioff);
441 return Nfs3ErrBadHandle;
444 if(debug) print("cg->ibno %lld ufs %d...", cg->ibno, fs->ufs);
445 iaddr = (cg->ibno+ioff/fs->inosperblock)*(vlong)fs->blocksize;
446 ioff = ioff%fs->inosperblock;
447 if((b = diskread(fs->disk, fs->blocksize, iaddr)) == nil)
450 *ino = ((Inode*)b->data)[ioff];
451 lastiaddr = iaddr+ioff*sizeof(Inode);
453 ino1 = ((Inode1*)b->data)[ioff];
454 inode1to2(&ino1, ino);
455 lastiaddr = iaddr+ioff*sizeof(Inode1);
462 ffsroot(Fsys *fsys, Nfs3Handle *h)
470 ino2attr(Ffs *fs, Inode *ino, u32int inum, Nfs3Attr *attr)
475 switch(ino->mode&IFMT){
477 attr->type = Nfs3FileFifo;
480 attr->type = Nfs3FileChar;
483 attr->type = Nfs3FileDir;
486 attr->type = Nfs3FileBlock;
489 attr->type = Nfs3FileReg;
492 attr->type = Nfs3FileSymlink;
495 attr->type = Nfs3FileSocket;
499 return Nfs3ErrBadHandle;
502 attr->mode = ino->mode&07777;
503 attr->nlink = ino->nlink;
504 attr->uid = ino->uid;
505 attr->gid = ino->gid;
506 attr->size = ino->size;
507 attr->used = ino->nblock*fs->blocksize;
508 if(attr->type==Nfs3FileBlock || attr->type==Nfs3FileChar){
510 attr->major = (rdev>>8)&0xFF;
511 attr->minor = rdev & 0xFFFF00FF;
518 attr->atime.sec = ino->atime;
519 attr->atime.nsec = ino->atimensec;
520 attr->mtime.sec = ino->mtime;
521 attr->mtime.nsec = ino->mtimensec;
522 attr->ctime.sec = ino->ctime;
523 attr->ctime.nsec = ino->ctimensec;
528 ingroup(SunAuthUnix *au, uint gid)
532 for(i=0; i<au->ng; i++)
539 inoperm(Inode *ino, SunAuthUnix *au, int need)
546 have = ino->mode&0777;
547 if(ino->uid == au->uid)
549 else if(ino->gid == au->gid || ingroup(au, ino->gid))
552 if((have&need) != need)
553 return Nfs3ErrNotOwner; /* really EPERM */
558 ffsgetattr(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, Nfs3Attr *attr)
566 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
569 USED(au); /* anyone can getattr */
571 return ino2attr(fs, &ino, inum, attr);
575 ffsaccess(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int want, u32int *got, Nfs3Attr *attr)
584 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
587 have = ino.mode&0777;
588 if(ino.uid == au->uid)
590 else if(ino.gid == au->gid || ingroup(au, ino.gid))
594 if((want&Nfs3AccessRead) && (have&AREAD))
595 *got |= Nfs3AccessRead;
596 if((want&Nfs3AccessLookup) && (ino.mode&IFMT)==IFDIR && (have&AEXEC))
597 *got |= Nfs3AccessLookup;
598 if((want&Nfs3AccessExecute) && (ino.mode&IFMT)!=IFDIR && (have&AEXEC))
599 *got |= Nfs3AccessExecute;
601 return ino2attr(fs, &ino, inum, attr);
605 ffslookup(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char *name, Nfs3Handle *nh)
618 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
621 if((ino.mode&IFMT) != IFDIR)
622 return Nfs3ErrNotDir;
624 if((ok = inoperm(&ino, au, AEXEC)) != Nfs3Ok)
628 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
629 for(i=0; i<nblock; i++){
631 want = ino.size % fs->blocksize;
633 want = fs->blocksize;
634 b = ffsfileblock(fs, &ino, i, want);
643 fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
649 fprint(2, "bad len %d at offset %d of %d\n", de->reclen, (int)(p-b->data), b->len);
654 if(4+2+2+de->namlen > de->reclen){
656 fprint(2, "bad namelen %d at offset %d of %d\n", de->namlen, (int)(p-b->data), b->len);
659 if(de->namlen == len && memcmp(de->name, name, len) == 0){
660 mkhandle(nh, de->ino);
671 ffsreaddir(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count, u64int cookie, uchar **pdata, u32int *pcount, u1int *peof)
676 uchar *data, *dp, *dep, *p, *ep, *ndp;
686 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
689 if((ino.mode&IFMT) != IFDIR)
690 return Nfs3ErrNotDir;
692 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
695 if(cookie >= ino.size){
707 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
708 i = cookie/fs->blocksize;
709 off = cookie%fs->blocksize;
711 for(; i<nblock && !done; i++){
713 want = ino.size % fs->blocksize;
715 want = fs->blocksize;
716 b = ffsfileblock(fs, &ino, i, want);
721 memset(&e, 0, sizeof e);
725 if(debug) fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
730 if(debug) fprint(2, "reclen %d at offset %d of %d\n", de->reclen, (int)(p-b->data), b->len);
734 if(debug) fprint(2, "zero inode\n");
737 if(4+2+2+de->namlen > de->reclen){
738 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);
741 if(de->name[de->namlen] != 0){
742 if(debug) fprint(2, "bad name %d %.*s\n", de->namlen, de->namlen, de->name);
745 if(debug) print("%s/%d ", de->name, (int)de->ino);
746 if((uchar*)de - b->data < off)
750 e.cookie = (u64int)i*fs->blocksize + (p - b->data);
751 if(nfs3entrypack(dp, dep, &ndp, &e) < 0){
769 ffsxfileblock(Fsys *fsys, Nfs3Handle *h, u64int offset)
777 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok){
781 if(offset == 1) /* clumsy hack for debugging */
783 if(offset >= ino.size){
784 werrstr("beyond end of file");
787 bno = offset/fs->blocksize;
788 bno = ffsfileblockno(fs, &ino, bno);
791 return bno*(u64int)fs->fragsize;
795 ffsreadfile(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count,
796 u64int offset, uchar **pdata, u32int *pcount, u1int *peof)
801 int off, want, fragcount;
806 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
809 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
812 if(offset >= ino.size){
818 if(offset+count > ino.size)
819 count = ino.size-offset;
820 if(offset/fs->blocksize != (offset+count-1)/fs->blocksize)
821 count = fs->blocksize - offset%fs->blocksize;
823 data = malloc(count);
827 want = offset%fs->blocksize+count;
828 if(want%fs->fragsize)
829 want += fs->fragsize - want%fs->fragsize;
831 b = ffsfileblock(fs, &ino, offset/fs->blocksize, want);
833 /* BUG: distinguish sparse file from I/O error */
834 memset(data, 0, count);
836 off = offset%fs->blocksize;
837 fragcount = count; /* need signed variable */
838 if(off+fragcount > b->len){
839 fragcount = b->len - off;
844 memmove(data, b->data+off, fragcount);
848 *peof = (offset+count == ino.size);
855 ffsreadlink(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char **link)
864 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
866 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
874 /* assumes symlink fits in one block */
875 b = ffsfileblock(fs, &ino, 0, len);
878 if(memchr(b->data, 0, len) != nil){
882 *link = malloc(len+1);
887 memmove(*link, b->data, len);
893 if(len > sizeof ino.db + sizeof ino.ib)
896 *link = malloc(len+1);
899 memmove(*link, ino.db, ino.size);