Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include <sunrpc.h>
5 #include <nfs3.h>
6 #include <diskfs.h>
7 #include "ffs.h"
9 #define BADBNO ((u64int)~0ULL)
11 #define checkcg 0
12 #define debug 0
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);
29 Fsys*
30 fsysopenffs(Disk *disk)
31 {
32 Ffs *fs;
33 Fsys *fsys;
35 fsys = emalloc(sizeof(Fsys));
36 fs = emalloc(sizeof(Ffs));
37 fs->disk = disk;
38 fsys->priv = fs;
39 fsys->type = "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;
51 if(ffssync(fsys) < 0)
52 goto error;
54 return fsys;
56 error:
57 ffsclose(fsys);
58 return nil;
59 }
61 static Cgblk*
62 ffscylgrp(Ffs *fs, u32int i, Block **pb)
63 {
64 Block *b;
65 Cgblk *cg;
67 if(i >= fs->ncg)
68 return nil;
70 b = diskread(fs->disk, fs->blocksize, (u64int)fs->cg[i].cgblkno*fs->blocksize);
71 if(b == nil)
72 return nil;
73 cg = (Cgblk*)b->data;
74 if(checkcgblk(cg) < 0){
75 fprint(2, "checkcgblk %d %lud: %r\n", i, (ulong)fs->cg[i].cgblkno);
76 blockput(b);
77 return nil;
78 }
79 *pb = b;
80 return cg;
81 }
83 static int
84 ffssync(Fsys *fsys)
85 {
86 int i;
87 int off[] = { SBOFF, SBOFF2, SBOFFPIGGY };
88 Block *b, *cgb;
89 Cgblk *cgblk;
90 Cylgrp *cg;
91 Disk *disk;
92 Ffs *fs;
93 Fsblk *fsblk;
95 fs = fsys->priv;
96 disk = fs->disk;
98 /*
99 * Read super block.
100 */
101 b = nil;
102 for(i=0; i<nelem(off); i++){
103 if((b = diskread(disk, SBSIZE, off[i])) == nil)
104 goto error;
105 fsblk = (Fsblk*)b->data;
106 // fprint(2, "offset of magic: %ld\n", offsetof(Fsblk, magic));
107 if((fs->ufs = checkfsblk(fsblk)) > 0)
108 goto okay;
109 blockput(b);
110 b = nil;
112 goto error;
114 okay:
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;
125 /*
126 * used to use
127 * fs->blockspergroup = (u64int)fsblk->_cylspergroup *
128 * fsblk->secspercyl * BYTESPERSEC / fsblk->blocksize;
129 * for UFS1, but this should work for both UFS1 and UFS2
130 */
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);
142 if(fs->cg == nil)
143 fs->cg = emalloc(fs->ncg*sizeof(Cylgrp));
144 for(i=0; i<fs->ncg; i++){
145 cg = &fs->cg[i];
146 if(fs->ufs == 2)
147 cg->bno = (u64int)fs->blockspergroup*i;
148 else
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;
154 if(checkcg){
155 if((cgb = diskread(disk, fs->blocksize, (u64int)cg->cgblkno*fs->blocksize)) == nil)
156 goto error;
158 cgblk = (Cgblk*)cgb->data;
159 if(checkcgblk(cgblk) < 0){
160 blockput(cgb);
161 goto error;
163 if(cgblk->nfrag % fs->fragsperblock && i != fs->ncg-1){
164 werrstr("fractional number of blocks in non-last cylinder group %d", cgblk->nfrag);
165 blockput(cgb);
166 goto error;
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); */
173 blockput(b);
174 return 0;
176 error:
177 blockput(b);
178 return -1;
181 static void
182 ffsclose(Fsys *fsys)
184 Ffs *fs;
186 fs = fsys->priv;
187 if(fs->cg)
188 free(fs->cg);
189 free(fs);
190 free(fsys);
193 static int
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;
202 return 1;
204 if(super->magic == FSMAGIC2){
205 return 2;
208 werrstr("bad super block");
209 return -1;
212 static int
213 checkcgblk(Cgblk *cg)
215 if(cg->magic != CGMAGIC){
216 werrstr("bad cylinder group block");
217 return -1;
219 return 0;
222 /*
223 * Read block #bno from the disk, zeroing unused data.
224 * If there is no data whatsoever, it's okay to return nil.
225 */
226 int nskipx;
227 static Block*
228 ffsblockread(Fsys *fsys, u64int bno)
230 int i, o;
231 u8int *fmap;
232 int frag, fsize, avail;
233 Block *b;
234 Cgblk *cgblk;
235 Ffs *fs;
237 fs = fsys->priv;
238 i = bno / fs->blockspergroup;
239 o = bno % fs->blockspergroup;
240 if(i >= fs->ncg)
241 return nil;
243 if((cgblk = ffscylgrp(fs, i, &b)) == nil)
244 return nil;
246 fmap = (u8int*)cgblk+cgblk->fmapoff;
247 frag = fs->fragsperblock;
248 switch(frag){
249 default:
250 sysfatal("bad frag");
251 case 8:
252 avail = fmap[o];
253 break;
254 case 4:
255 avail = (fmap[o>>1] >> ((o&1)*4)) & 0xF;
256 break;
257 case 2:
258 avail = (fmap[o>>2] >> ((o&3)*2)) & 0x3;
259 break;
260 case 1:
261 avail = (fmap[o>>3] >> (o&7)) & 0x1;
262 break;
264 blockput(b);
266 if(avail == ((1<<frag)-1))
268 nskipx++;
269 return nil;
271 if((b = diskread(fs->disk, fs->blocksize, bno*fs->blocksize)) == nil){
272 fprint(2, "diskread failed!!!\n");
273 return nil;
276 fsize = fs->fragsize;
277 for(i=0; i<frag; i++)
278 if(avail & (1<<i))
279 memset(b->data + fsize*i, 0, fsize);
280 return b;
283 static Block*
284 ffsdatablock(Ffs *fs, u64int bno, int size)
286 int fsize;
287 u64int diskaddr;
288 Block *b;
290 if(bno == 0)
291 return nil;
293 fsize = 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);
299 return nil;
301 diskaddr = (u64int)bno*fs->fragsize;
302 b = diskread(fs->disk, fsize, diskaddr);
303 if(b == nil){
304 fprint(2, "ffs: disk i/o at %#llux for %#ux: %r\n", diskaddr, fsize);
305 return nil;
307 if(b->len < fsize){
308 fprint(2, "ffs: disk i/o at %#llux for %#ux got %#ux\n", diskaddr, fsize,
309 b->len);
310 blockput(b);
311 return nil;
314 return b;
317 static u64int
318 ifetch(Ffs *fs, u64int bno, u32int off)
320 Block *b;
322 if(bno == BADBNO)
323 return BADBNO;
324 b = ffsdatablock(fs, bno, fs->blocksize);
325 if(b == nil)
326 return BADBNO;
327 if(fs->ufs == 2)
328 bno = ((u64int*)b->data)[off];
329 else
330 bno = ((u32int*)b->data)[off];
331 blockput(b);
332 return bno;
335 static u64int
336 ffsfileblockno(Ffs *fs, Inode *ino, u64int bno)
338 int ppb;
340 if(bno < NDADDR){
341 if(debug) fprint(2, "ffsfileblock %lud: direct %#lux\n", (ulong)bno, (ulong)ino->db[bno]);
342 return ino->db[bno];
344 bno -= NDADDR;
345 ppb = fs->blocksize/4;
347 if(bno < ppb) /* single indirect */
348 return ifetch(fs, ino->ib[0], bno);
349 bno -= ppb;
351 if(bno < ppb*ppb)
352 return ifetch(fs, ifetch(fs, ino->ib[1], bno/ppb), bno%ppb);
353 bno -= ppb*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);
359 return BADBNO;
362 static Block*
363 ffsfileblock(Ffs *fs, Inode *ino, u64int bno, int size)
365 u64int b;
367 b = ffsfileblockno(fs, ino, bno);
368 if(b == ~0)
369 return nil;
370 return ffsdatablock(fs, b, size);
373 /*
374 * NFS handles are 4-byte inode number.
375 */
376 static void
377 mkhandle(Nfs3Handle *h, u64int ino)
379 h->h[0] = ino >> 24;
380 h->h[1] = ino >> 16;
381 h->h[2] = ino >> 8;
382 h->h[3] = ino;
383 h->len = 4;
386 static u32int
387 byte2u32(uchar *p)
389 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
392 static u64int lastiaddr; /* debugging */
394 static void
395 inode1to2(Inode1 *i1, Inode *i2)
397 int i;
399 memset(i2, 0, sizeof *i2);
400 i2->mode = i1->mode;
401 i2->nlink = i1->nlink;
402 i2->size = i1->size;
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;
415 i2->gen = i1->gen;
416 i2->uid = i1->uid;
417 i2->gid = i1->gid;
420 static Nfs3Status
421 handle2ino(Ffs *fs, Nfs3Handle *h, u32int *pinum, Inode *ino)
423 int i;
424 u32int ioff;
425 u32int inum;
426 u64int iaddr;
427 Block *b;
428 Cylgrp *cg;
429 Inode1 ino1;
431 if(h->len != 4)
432 return Nfs3ErrBadHandle;
433 inum = byte2u32(h->h);
434 if(pinum)
435 *pinum = inum;
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);
442 if(i >= fs->ncg)
443 return Nfs3ErrBadHandle;
444 cg = &fs->cg[i];
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)
450 return Nfs3ErrIo;
451 if(fs->ufs == 2){
452 *ino = ((Inode*)b->data)[ioff];
453 lastiaddr = iaddr+ioff*sizeof(Inode);
454 }else{
455 ino1 = ((Inode1*)b->data)[ioff];
456 inode1to2(&ino1, ino);
457 lastiaddr = iaddr+ioff*sizeof(Inode1);
459 blockput(b);
460 return Nfs3Ok;
463 static Nfs3Status
464 ffsroot(Fsys *fsys, Nfs3Handle *h)
466 USED(fsys);
467 mkhandle(h, 2);
468 return Nfs3Ok;
471 static Nfs3Status
472 ino2attr(Ffs *fs, Inode *ino, u32int inum, Nfs3Attr *attr)
474 u32int rdev;
476 attr->type = -1;
477 switch(ino->mode&IFMT){
478 case IFIFO:
479 attr->type = Nfs3FileFifo;
480 break;
481 case IFCHR:
482 attr->type = Nfs3FileChar;
483 break;
484 case IFDIR:
485 attr->type = Nfs3FileDir;
486 break;
487 case IFBLK:
488 attr->type = Nfs3FileBlock;
489 break;
490 case IFREG:
491 attr->type = Nfs3FileReg;
492 break;
493 case IFLNK:
494 attr->type = Nfs3FileSymlink;
495 break;
496 case IFSOCK:
497 attr->type = Nfs3FileSocket;
498 break;
499 case IFWHT:
500 default:
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){
511 rdev = ino->db[0];
512 attr->major = (rdev>>8)&0xFF;
513 attr->minor = rdev & 0xFFFF00FF;
514 }else{
515 attr->major = 0;
516 attr->minor = 0;
518 attr->fsid = 0;
519 attr->fileid = inum;
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;
526 return Nfs3Ok;
529 static int
530 ingroup(SunAuthUnix *au, uint gid)
532 int i;
534 for(i=0; i<au->ng; i++)
535 if(au->g[i] == gid)
536 return 1;
537 return 0;
540 static Nfs3Status
541 inoperm(Inode *ino, SunAuthUnix *au, int need)
543 int have;
545 if(au == nil)
546 return Nfs3Ok;
548 have = ino->mode&0777;
549 if(ino->uid == au->uid)
550 have >>= 6;
551 else if(ino->gid == au->gid || ingroup(au, ino->gid))
552 have >>= 3;
554 if((have&need) != need)
555 return Nfs3ErrNotOwner; /* really EPERM */
556 return Nfs3Ok;
559 static Nfs3Status
560 ffsgetattr(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, Nfs3Attr *attr)
562 Inode ino;
563 u32int inum;
564 Ffs *fs;
565 Nfs3Status ok;
567 fs = fsys->priv;
568 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
569 return ok;
571 USED(au); /* anyone can getattr */
573 return ino2attr(fs, &ino, inum, attr);
576 static Nfs3Status
577 ffsaccess(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int want, u32int *got, Nfs3Attr *attr)
579 int have;
580 Inode ino;
581 u32int inum;
582 Ffs *fs;
583 Nfs3Status ok;
585 fs = fsys->priv;
586 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
587 return ok;
589 have = ino.mode&0777;
590 if(ino.uid == au->uid)
591 have >>= 6;
592 else if(ino.gid == au->gid || ingroup(au, ino.gid))
593 have >>= 3;
595 *got = 0;
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);
606 static Nfs3Status
607 ffslookup(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char *name, Nfs3Handle *nh)
609 u32int nblock;
610 u32int i;
611 uchar *p, *ep;
612 Dirent *de;
613 Inode ino;
614 Block *b;
615 Ffs *fs;
616 Nfs3Status ok;
617 int len, want;
619 fs = fsys->priv;
620 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
621 return ok;
623 if((ino.mode&IFMT) != IFDIR)
624 return Nfs3ErrNotDir;
626 if((ok = inoperm(&ino, au, AEXEC)) != Nfs3Ok)
627 return ok;
629 len = strlen(name);
630 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
631 for(i=0; i<nblock; i++){
632 if(i==nblock-1)
633 want = ino.size % fs->blocksize;
634 else
635 want = fs->blocksize;
636 b = ffsfileblock(fs, &ino, i, want);
637 if(b == nil)
638 continue;
639 p = b->data;
640 ep = p+b->len;
641 while(p < ep){
642 de = (Dirent*)p;
643 if(de->reclen == 0){
644 if(debug)
645 fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
646 break;
648 p += de->reclen;
649 if(p > ep){
650 if(debug)
651 fprint(2, "bad len %d at offset %d of %d\n", de->reclen, (int)(p-b->data), b->len);
652 break;
654 if(de->ino == 0)
655 continue;
656 if(4+2+2+de->namlen > de->reclen){
657 if(debug)
658 fprint(2, "bad namelen %d at offset %d of %d\n", de->namlen, (int)(p-b->data), b->len);
659 break;
661 if(de->namlen == len && memcmp(de->name, name, len) == 0){
662 mkhandle(nh, de->ino);
663 blockput(b);
664 return Nfs3Ok;
667 blockput(b);
669 return Nfs3ErrNoEnt;
672 static Nfs3Status
673 ffsreaddir(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count, u64int cookie, uchar **pdata, u32int *pcount, u1int *peof)
675 u32int nblock;
676 u32int i;
677 int off, done;
678 uchar *data, *dp, *dep, *p, *ep, *ndp;
679 Dirent *de;
680 Inode ino;
681 Block *b;
682 Ffs *fs;
683 Nfs3Status ok;
684 Nfs3Entry e;
685 int want;
687 fs = fsys->priv;
688 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
689 return ok;
691 if((ino.mode&IFMT) != IFDIR)
692 return Nfs3ErrNotDir;
694 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
695 return ok;
697 if(cookie >= ino.size){
698 *pcount = 0;
699 *pdata = 0;
700 return Nfs3Ok;
703 dp = malloc(count);
704 data = dp;
705 if(dp == nil)
706 return Nfs3ErrNoMem;
707 dep = dp+count;
708 *peof = 0;
709 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
710 i = cookie/fs->blocksize;
711 off = cookie%fs->blocksize;
712 done = 0;
713 for(; i<nblock && !done; i++){
714 if(i==nblock-1)
715 want = ino.size % fs->blocksize;
716 else
717 want = fs->blocksize;
718 b = ffsfileblock(fs, &ino, i, want);
719 if(b == nil)
720 continue;
721 p = b->data;
722 ep = p+b->len;
723 memset(&e, 0, sizeof e);
724 while(p < ep){
725 de = (Dirent*)p;
726 if(de->reclen == 0){
727 if(debug) fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
728 break;
730 p += de->reclen;
731 if(p > ep){
732 if(debug) fprint(2, "reclen %d at offset %d of %d\n", de->reclen, (int)(p-b->data), b->len);
733 break;
735 if(de->ino == 0){
736 if(debug) fprint(2, "zero inode\n");
737 continue;
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);
741 break;
743 if(de->name[de->namlen] != 0){
744 if(debug) fprint(2, "bad name %d %.*s\n", de->namlen, de->namlen, de->name);
745 continue;
747 if(debug) print("%s/%d ", de->name, (int)de->ino);
748 if((uchar*)de - b->data < off)
749 continue;
750 e.fileid = de->ino;
751 e.name = de->name;
752 e.namelen = de->namlen;
753 e.cookie = (u64int)i*fs->blocksize + (p - b->data);
754 if(nfs3entrypack(dp, dep, &ndp, &e) < 0){
755 done = 1;
756 break;
758 dp = ndp;
760 off = 0;
761 blockput(b);
763 if(i==nblock)
764 *peof = 1;
766 *pcount = dp - data;
767 *pdata = data;
768 return Nfs3Ok;
771 static u64int
772 ffsxfileblock(Fsys *fsys, Nfs3Handle *h, u64int offset)
774 u64int bno;
775 Inode ino;
776 Nfs3Status ok;
777 Ffs *fs;
779 fs = fsys->priv;
780 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok){
781 nfs3errstr(ok);
782 return 0;
784 if(offset == 1) /* clumsy hack for debugging */
785 return lastiaddr;
786 if(offset >= ino.size){
787 werrstr("beyond end of file");
788 return 0;
790 bno = offset/fs->blocksize;
791 bno = ffsfileblockno(fs, &ino, bno);
792 if(bno == ~0)
793 return 0;
794 return bno*(u64int)fs->fragsize;
797 static Nfs3Status
798 ffsreadfile(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count,
799 u64int offset, uchar **pdata, u32int *pcount, u1int *peof)
801 uchar *data;
802 Block *b;
803 Ffs *fs;
804 int off, want, fragcount;
805 Inode ino;
806 Nfs3Status ok;
808 fs = fsys->priv;
809 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
810 return ok;
812 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
813 return ok;
815 if(offset >= ino.size){
816 *pdata = 0;
817 *pcount = 0;
818 *peof = 1;
819 return Nfs3Ok;
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);
827 if(data == nil)
828 return Nfs3ErrNoMem;
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);
835 if(b == nil){
836 /* BUG: distinguish sparse file from I/O error */
837 memset(data, 0, count);
838 }else{
839 off = offset%fs->blocksize;
840 fragcount = count; /* need signed variable */
841 if(off+fragcount > b->len){
842 fragcount = b->len - off;
843 if(fragcount < 0)
844 fragcount = 0;
846 if(fragcount > 0)
847 memmove(data, b->data+off, fragcount);
848 count = fragcount;
849 blockput(b);
851 *peof = (offset+count == ino.size);
852 *pcount = count;
853 *pdata = data;
854 return Nfs3Ok;
857 static Nfs3Status
858 ffsreadlink(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char **link)
860 Ffs *fs;
861 Nfs3Status ok;
862 int len;
863 Inode ino;
864 Block *b;
866 fs = fsys->priv;
867 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
868 return ok;
869 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
870 return ok;
872 if(ino.size > 1024)
873 return Nfs3ErrIo;
874 len = ino.size;
876 if(ino.nblock != 0){
877 /* assumes symlink fits in one block */
878 b = ffsfileblock(fs, &ino, 0, len);
879 if(b == nil)
880 return Nfs3ErrIo;
881 if(memchr(b->data, 0, len) != nil){
882 blockput(b);
883 return Nfs3ErrIo;
885 *link = malloc(len+1);
886 if(*link == 0){
887 blockput(b);
888 return Nfs3ErrNoMem;
890 memmove(*link, b->data, len);
891 (*link)[len] = 0;
892 blockput(b);
893 return Nfs3Ok;
896 if(len > sizeof ino.db + sizeof ino.ib)
897 return Nfs3ErrIo;
899 *link = malloc(len+1);
900 if(*link == 0)
901 return Nfs3ErrNoMem;
902 memmove(*link, ino.db, ino.size);
903 (*link)[len] = 0;
904 return Nfs3Ok;