12 static int checkfsblk(Fsblk*);
13 static int checkcgblk(Cgblk*);
14 static Block *ffsblockread(Fsys*, u64int);
15 static int ffssync(Fsys*);
16 static void ffsclose(Fsys*);
18 static Nfs3Status ffsroot(Fsys*, Nfs3Handle*);
19 static Nfs3Status ffsgetattr(Fsys*, SunAuthUnix *au, Nfs3Handle*, Nfs3Attr*);
20 static Nfs3Status ffslookup(Fsys*, SunAuthUnix *au, Nfs3Handle*, char*, Nfs3Handle*);
21 static Nfs3Status ffsreadfile(Fsys*, SunAuthUnix *au, Nfs3Handle*, u32int, u64int, uchar**, u32int*, u1int*);
22 static Nfs3Status ffsreadlink(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char **link);
23 static Nfs3Status ffsreaddir(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int, u64int, uchar**, u32int*, u1int*);
24 static Nfs3Status ffsaccess(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int want, u32int *got, Nfs3Attr *attr);
27 fsysopenffs(Disk *disk)
32 fsys = emalloc(sizeof(Fsys));
33 fs = emalloc(sizeof(Ffs));
37 fsys->_readblock = ffsblockread;
38 fsys->_sync = ffssync;
39 fsys->_root = ffsroot;
40 fsys->_getattr = ffsgetattr;
41 fsys->_access = ffsaccess;
42 fsys->_lookup = ffslookup;
43 fsys->_readfile = ffsreadfile;
44 fsys->_readlink = ffsreadlink;
45 fsys->_readdir = ffsreaddir;
58 ffscylgrp(Ffs *fs, int i, Block **pb)
66 b = diskread(fs->disk, fs->blocksize, (u64int)fs->cg[i].cgblkno*fs->blocksize);
70 if(checkcgblk(cg) < 0){
71 fprint(2, "checkcgblk %d %lud: %r\n", i, (ulong)fs->cg[i].cgblkno);
96 if((b = diskread(disk, SBSIZE, SBOFF)) == nil)
98 fsblk = (Fsblk*)b->data;
99 if(checkfsblk(fsblk) < 0)
102 fs->blocksize = fsblk->blocksize;
103 fs->nblock = (fsblk->nfrag+fsblk->fragsperblock-1) / fsblk->fragsperblock;
104 fs->fragsize = fsblk->fragsize;
105 fs->fragspergroup = fsblk->fragspergroup;
106 fs->fragsperblock = fsblk->fragsperblock;
107 fs->inosperblock = fsblk->inosperblock;
108 fs->inospergroup = fsblk->inospergroup;
110 fs->nfrag = fsblk->nfrag;
111 fs->ndfrag = fsblk->ndfrag;
112 fs->blockspergroup = (u64int)fsblk->cylspergroup *
113 fsblk->secspercyl * BYTESPERSEC / fsblk->blocksize;
114 fs->ncg = fsblk->ncg;
116 fsys->blocksize = fs->blocksize;
117 fsys->nblock = fs->nblock;
119 if(0) fprint(2, "ffs %d %d-byte blocks, %d cylinder groups\n",
120 fs->nblock, fs->blocksize, fs->ncg);
123 fs->cg = emalloc(fs->ncg*sizeof(Cylgrp));
124 for(i=0; i<fs->ncg; i++){
126 cg->bno = fs->blockspergroup*i + fsblk->cgoffset * (i & ~fsblk->cgmask);
127 cg->cgblkno = cg->bno + fsblk->cfragno/fs->fragsperblock;
128 cg->ibno = cg->bno + fsblk->ifragno/fs->fragsperblock;
129 cg->dbno = cg->bno + fsblk->dfragno/fs->fragsperblock;
132 if((cgb = diskread(disk, fs->blocksize, (u64int)cg->cgblkno*fs->blocksize)) == nil)
135 cgblk = (Cgblk*)cgb->data;
136 if(checkcgblk(cgblk) < 0){
140 if(cgblk->nfrag % fs->fragsperblock && i != fs->ncg-1){
141 werrstr("fractional number of blocks in non-last cylinder group %d", cgblk->nfrag);
145 // cg->nfrag = cgblk->nfrag;
146 // cg->nblock = (cgblk->nfrag+fs->fragsperblock-1) / fs->fragsperblock;
147 // fprint(2, "cg #%d: cgblk %lud, %d blocks, %d inodes\n", cgblk->num, (ulong)cg->cgblkno, cg->nblock, cg->nino);
171 checkfsblk(Fsblk *super)
173 if(super->magic != FSMAGIC){
174 werrstr("bad super block");
182 checkcgblk(Cgblk *cg)
184 if(cg->magic != CGMAGIC){
185 werrstr("bad cylinder group block");
192 * Read block #bno from the disk, zeroing unused data.
193 * If there is no data whatsoever, it's okay to return nil.
197 ffsblockread(Fsys *fsys, u64int bno)
201 int frag, fsize, avail;
208 i = bno / fs->blockspergroup;
209 o = bno % fs->blockspergroup;
214 // if(o >= cg->nblock)
217 if((cgblk = ffscylgrp(fs, i, &b)) == nil)
220 fmap = (u8int*)cgblk+cgblk->fmapoff;
221 frag = fs->fragsperblock;
224 sysfatal("bad frag");
229 avail = (fmap[o>>1] >> ((o&1)*4)) & 0xF;
232 avail = (fmap[o>>2] >> ((o&3)*2)) & 0x3;
235 avail = (fmap[o>>3] >> (o&7)) & 0x1;
240 if(avail == ((1<<frag)-1))
245 if((b = diskread(fs->disk, fs->blocksize, bno*fs->blocksize)) == nil){
246 fprint(2, "diskread failed!!!\n");
250 fsize = fs->fragsize;
251 for(i=0; i<frag; i++)
253 memset(b->data + fsize*i, 0, fsize);
258 ffsdatablock(Ffs *fs, u32int bno, int size)
268 if(fsize < fs->fragsize)
269 fsize = fs->fragsize;
271 if(bno >= fs->nfrag){
272 fprint(2, "ffs: request for block %#lux; nfrag %#x\n", (ulong)bno, fs->nfrag);
275 diskaddr = (u64int)bno*fs->fragsize;
276 b = diskread(fs->disk, fsize, diskaddr);
278 fprint(2, "ffs: disk i/o at %#llux for %#ux: %r\n", diskaddr, fsize);
282 fprint(2, "ffs: disk i/o at %#llux for %#ux got %#ux\n", diskaddr, fsize,
292 ffsfileblock(Ffs *fs, Inode *ino, u32int bno, int size)
299 if(debug) fprint(2, "ffsfileblock %lud: direct %#lux\n", (ulong)bno, (ulong)ino->db[bno]);
300 return ffsdatablock(fs, ino->db[bno], size);
303 ppb = fs->blocksize/4;
305 if(bno/ppb < NIADDR){
306 if(debug) fprint(2, "ffsfileblock %lud: indirect %#lux\n", (ulong)(bno+NDADDR),
307 (ulong)ino->ib[bno/ppb]);
308 b = ffsdatablock(fs, ino->ib[bno/ppb], fs->blocksize);
311 a = (u32int*)b->data;
313 if(debug) fprint(2, "ffsfileblock: indirect fetch %#lux size %d\n", (ulong)bno, size);
315 return ffsdatablock(fs, bno, size);
318 fprint(2, "ffsfileblock %lud: too big\n", (ulong)bno+NDADDR);
323 * NFS handles are 4-byte inode number.
326 mkhandle(Nfs3Handle *h, u64int ino)
338 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
342 handle2ino(Ffs *fs, Nfs3Handle *h, u32int *pinum, Inode *ino)
351 return Nfs3ErrBadHandle;
352 inum = byte2u32(h->h);
355 if(debug) print("inum %d...", (int)inum);
357 /* fetch inode from disk */
358 i = inum / fs->inospergroup;
359 ioff = inum % fs->inospergroup;
360 if(debug)print("cg %d off %d...", i, (int)ioff);
362 return Nfs3ErrBadHandle;
366 return Nfs3ErrBadHandle;
369 if(debug) print("cg->ibno %d...", cg->ibno);
370 if((b = diskread(fs->disk, fs->blocksize,
371 (cg->ibno+ioff/fs->inosperblock)*(vlong)fs->blocksize)) == nil)
373 *ino = ((Inode*)b->data)[ioff%fs->inosperblock];
380 ffsroot(Fsys *fsys, Nfs3Handle *h)
388 ino2attr(Ffs *fs, Inode *ino, u32int inum, Nfs3Attr *attr)
393 switch(ino->mode&IFMT){
395 attr->type = Nfs3FileFifo;
398 attr->type = Nfs3FileChar;
401 attr->type = Nfs3FileDir;
404 attr->type = Nfs3FileBlock;
407 attr->type = Nfs3FileReg;
410 attr->type = Nfs3FileSymlink;
413 attr->type = Nfs3FileSocket;
417 return Nfs3ErrBadHandle;
420 attr->mode = ino->mode&07777;
421 attr->nlink = ino->nlink;
422 attr->uid = ino->uid;
423 attr->gid = ino->gid;
424 attr->size = ino->size;
425 attr->used = ino->nblock*fs->blocksize;
426 if(attr->type==Nfs3FileBlock || attr->type==Nfs3FileChar){
428 attr->major = (rdev>>8)&0xFF;
429 attr->minor = rdev & 0xFFFF00FF;
436 attr->atime.sec = ino->atime;
437 attr->atime.nsec = ino->atimensec;
438 attr->mtime.sec = ino->mtime;
439 attr->mtime.nsec = ino->mtimensec;
440 attr->ctime.sec = ino->ctime;
441 attr->ctime.nsec = ino->ctimensec;
446 ingroup(SunAuthUnix *au, uint gid)
450 for(i=0; i<au->ng; i++)
457 inoperm(Inode *ino, SunAuthUnix *au, int need)
461 have = ino->mode&0777;
462 if(ino->uid == au->uid)
464 else if(ino->gid == au->gid || ingroup(au, ino->gid))
467 if((have&need) != need)
468 return Nfs3ErrNotOwner; /* really EPERM */
473 ffsgetattr(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, Nfs3Attr *attr)
481 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
484 USED(au); /* anyone can getattr */
486 return ino2attr(fs, &ino, inum, attr);
490 ffsaccess(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int want, u32int *got, Nfs3Attr *attr)
499 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
502 have = ino.mode&0777;
503 if(ino.uid == au->uid)
505 else if(ino.gid == au->gid || ingroup(au, ino.gid))
509 if((want&Nfs3AccessRead) && (have&AREAD))
510 *got |= Nfs3AccessRead;
511 if((want&Nfs3AccessLookup) && (ino.mode&IFMT)==IFDIR && (have&AEXEC))
512 *got |= Nfs3AccessLookup;
513 if((want&Nfs3AccessExecute) && (ino.mode&IFMT)!=IFDIR && (have&AEXEC))
514 *got |= Nfs3AccessExecute;
516 return ino2attr(fs, &ino, inum, attr);
520 ffslookup(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char *name, Nfs3Handle *nh)
533 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
536 if((ino.mode&IFMT) != IFDIR)
537 return Nfs3ErrNotDir;
539 if((ok = inoperm(&ino, au, AEXEC)) != Nfs3Ok)
543 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
544 for(i=0; i<nblock; i++){
546 want = ino.size % fs->blocksize;
548 want = fs->blocksize;
549 b = ffsfileblock(fs, &ino, i, want);
558 fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
564 fprint(2, "bad len %d at offset %d of %d\n", de->reclen, (int)(p-b->data), b->len);
569 if(4+2+2+de->namlen > de->reclen){
571 fprint(2, "bad namelen %d at offset %d of %d\n", de->namlen, (int)(p-b->data), b->len);
574 if(de->namlen == len && memcmp(de->name, name, len) == 0){
575 mkhandle(nh, de->ino);
586 ffsreaddir(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count, u64int cookie, uchar **pdata, u32int *pcount, u1int *peof)
591 uchar *data, *dp, *dep, *p, *ep, *ndp;
601 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
604 if((ino.mode&IFMT) != IFDIR)
605 return Nfs3ErrNotDir;
607 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
610 if(cookie >= ino.size){
622 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
623 i = cookie/fs->blocksize;
624 off = cookie%fs->blocksize;
626 for(; i<nblock && !done; i++){
628 want = ino.size % fs->blocksize;
630 want = fs->blocksize;
631 b = ffsfileblock(fs, &ino, i, want);
636 memset(&e, 0, sizeof e);
640 if(debug) fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
645 if(debug) fprint(2, "reclen %d at offset %d of %d\n", de->reclen, (int)(p-b->data), b->len);
649 if(debug) fprint(2, "zero inode\n");
652 if(4+2+2+de->namlen > de->reclen){
653 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);
656 if(de->name[de->namlen] != 0){
657 if(debug) fprint(2, "bad name %d %.*s\n", de->namlen, de->namlen, de->name);
660 if(debug) print("%s/%d ", de->name, (int)de->ino);
661 if((uchar*)de - b->data < off)
665 e.cookie = (u64int)i*fs->blocksize + (p - b->data);
666 if(nfs3entrypack(dp, dep, &ndp, &e) < 0){
684 ffsreadfile(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count,
685 u64int offset, uchar **pdata, u32int *pcount, u1int *peof)
690 int off, want, fragcount;
695 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
698 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
701 if(offset >= ino.size){
707 if(offset+count > ino.size)
708 count = ino.size-offset;
709 if(offset/fs->blocksize != (offset+count-1)/fs->blocksize)
710 count = fs->blocksize - offset%fs->blocksize;
712 data = malloc(count);
716 want = offset%fs->blocksize+count;
717 if(want%fs->fragsize)
718 want += fs->fragsize - want%fs->fragsize;
720 b = ffsfileblock(fs, &ino, offset/fs->blocksize, want);
722 /* BUG: distinguish sparse file from I/O error */
723 memset(data, 0, count);
725 off = offset%fs->blocksize;
726 fragcount = count; /* need signed variable */
727 if(off+fragcount > b->len){
728 fragcount = b->len - off;
733 memmove(data, b->data+off, fragcount);
737 *peof = (offset+count == ino.size);
744 ffsreadlink(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char **link)
753 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
755 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
763 /* BUG: assumes symlink fits in one block */
764 b = ffsfileblock(fs, &ino, 0, len);
767 if(memchr(b->data, 0, len) != nil){
771 *link = malloc(len+1);
776 memmove(*link, b->data, len);
782 if(len > sizeof ino.db + sizeof ino.ib)
785 *link = malloc(len+1);
788 memmove(*link, ino.db, ino.size);