/* * are things locked??? they need to be to avoid this being * deleted or changed (data block pointer blocks moving about.) */ int adosfs_read(void *v) { struct vop_read_args /* { struct vnode *a_vp; struct uio *a_uio; int a_ioflag; kauth_cred_t a_cred; } */ *sp = v; struct vnode *vp = sp->a_vp; struct adosfsmount *amp; struct anode *ap; struct uio *uio; struct buf *bp; daddr_t lbn; int size, diff, error; long n, on; #ifdef ADOSFS_DIAGNOSTIC advopprint(sp); #endif error = 0; uio = sp->a_uio; ap = VTOA(sp->a_vp); amp = ap->amp; /* * Return EOF for character devices, EIO for others */ if (sp->a_vp->v_type != VREG) { error = EIO; goto reterr; } if (uio->uio_resid == 0) goto reterr; if (uio->uio_offset < 0) { error = EINVAL; goto reterr; } /* * to expensive to let general algorithm figure out that * we are beyond the file. Do it now. */ if (uio->uio_offset >= ap->fsize) goto reterr; /* * taken from ufs_read() */ if (vp->v_type == VREG && IS_FFS(amp)) { const int advice = IO_ADV_DECODE(sp->a_ioflag); error = 0; while (uio->uio_resid > 0) { vsize_t bytelen = MIN(ap->fsize - uio->uio_offset, uio->uio_resid); if (bytelen == 0) { break; } error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice, UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp)); if (error) { break; } } goto out; } do { size = amp->dbsize; lbn = uio->uio_offset / size; on = uio->uio_offset % size; n = MIN(size - on, uio->uio_resid); diff = ap->fsize - uio->uio_offset; /* * check for EOF */ if (diff <= 0) return(0); if (diff < n) n = diff; /* * read ahead could possibly be worth something * but not much as ados makes little attempt to * make things contigous */ error = bread(sp->a_vp, lbn, amp->bsize, 0, &bp); if (error) { goto reterr; } if (!IS_FFS(amp)) { if (bp->b_resid > 0) error = EIO; /* OFS needs the complete block */ else if (adoswordn(bp, 0) != BPT_DATA) { #ifdef DIAGNOSTIC printf("adosfs: bad primary type blk %" PRId64 "\n", bp->b_blkno / (amp->bsize / DEV_BSIZE)); #endif error = EINVAL; } else if (adoscksum(bp, ap->nwords)) { #ifdef DIAGNOSTIC printf("adosfs: blk %" PRId64 " failed cksum.\n", bp->b_blkno / (amp->bsize / DEV_BSIZE)); #endif error = EINVAL; } } if (error) { brelse(bp, 0); goto reterr; } #ifdef ADOSFS_DIAGNOSTIC printf(" %" PRId64 "+%ld-%" PRId64 "+%ld", lbn, on, lbn, n); #endif n = MIN(n, size - bp->b_resid); error = uiomove((char *)bp->b_data + on + amp->bsize - amp->dbsize, (int)n, uio); brelse(bp, 0); } while (error == 0 && uio->uio_resid > 0 && n != 0); out: reterr: #ifdef ADOSFS_DIAGNOSTIC printf(" %d)", error); #endif return(error); }
/* * Wait until the vnode has finished changing state. */ int adosfs_bmap(void *v) { struct vop_bmap_args /* { struct vnode *a_vp; daddr_t a_bn; struct vnode **a_vpp; daddr_t *a_bnp; int *a_runp; } */ *sp = v; struct anode *ap; struct buf *flbp; long nb, flblk, flblkoff, fcnt; daddr_t *bnp; daddr_t bn; int error; #ifdef ADOSFS_DIAGNOSTIC advopprint(sp); #endif ap = VTOA(sp->a_vp); bn = sp->a_bn; bnp = sp->a_bnp; if (sp->a_runp) { *sp->a_runp = 0; } error = 0; if (sp->a_vpp != NULL) *sp->a_vpp = ap->amp->devvp; if (bnp == NULL) goto reterr; if (bn < 0) { error = EFBIG; goto reterr; } if (sp->a_vp->v_type != VREG) { error = EINVAL; goto reterr; } /* * walk the chain of file list blocks until we find * the one that will yield the block pointer we need. */ if (ap->type == AFILE) nb = ap->block; /* pointer to ourself */ else if (ap->type == ALFILE) nb = ap->linkto; /* pointer to real file */ else { error = EINVAL; goto reterr; } flblk = bn / ANODENDATBLKENT(ap); flbp = NULL; /* * check last indirect block cache */ if (flblk < ap->lastlindblk) fcnt = 0; else { flblk -= ap->lastlindblk; fcnt = ap->lastlindblk; nb = ap->lastindblk; } while (flblk >= 0) { if (flbp) brelse(flbp, 0); if (nb == 0) { #ifdef DIAGNOSTIC printf("adosfs: bad file list chain.\n"); #endif error = EINVAL; goto reterr; } error = bread(ap->amp->devvp, nb * ap->amp->bsize / DEV_BSIZE, ap->amp->bsize, 0, &flbp); if (error) { goto reterr; } if (adoscksum(flbp, ap->nwords)) { #ifdef DIAGNOSTIC printf("adosfs: blk %ld failed cksum.\n", nb); #endif brelse(flbp, 0); error = EINVAL; goto reterr; } /* * update last indirect block cache */ ap->lastlindblk = fcnt++; ap->lastindblk = nb; nb = adoswordn(flbp, ap->nwords - 2); flblk--; } /* * calculate offset of block number in table. The table starts * at nwords - 51 and goes to offset 6 or less if indicated by the * valid table entries stored at offset ADBI_NBLKTABENT. */ flblkoff = bn % ANODENDATBLKENT(ap); if (flblkoff < adoswordn(flbp, 2 /* ADBI_NBLKTABENT */)) { flblkoff = (ap->nwords - 51) - flblkoff; *bnp = adoswordn(flbp, flblkoff) * ap->amp->bsize / DEV_BSIZE; } else { #ifdef DIAGNOSTIC printf("flblk offset %ld too large in lblk %ld blk %" PRId64 "\n", flblkoff, (long)bn, flbp->b_blkno); #endif error = EINVAL; } brelse(flbp, 0); reterr: #ifdef ADOSFS_DIAGNOSTIC if (error == 0 && bnp) printf(" %lld => %lld", (long long)bn, (long long)*bnp); printf(" %d)\n", error); #endif return(error); }
/* * Load the bitmap into memory, and count the number of available * blocks. * The bitmap will be released if the filesystem is read-only; it's * only needed to find the free space. */ int adosfs_loadbitmap(struct adosfsmount *amp) { struct buf *bp, *mapbp; u_long bn; int blkix, endix, mapix; int bmsize; int error; bp = mapbp = NULL; bn = amp->rootb; if ((error = bread(amp->devvp, bn * amp->bsize / DEV_BSIZE, amp->bsize, NOCRED, 0, &bp)) != 0) { return (error); } blkix = amp->nwords - 49; endix = amp->nwords - 24; mapix = 0; bmsize = (amp->numblks + 31) / 32; while (mapix < bmsize) { int n; u_long bits; if (adoswordn(bp, blkix) == 0) break; if (mapbp != NULL) brelse(mapbp, 0); if ((error = bread(amp->devvp, adoswordn(bp, blkix) * amp->bsize / DEV_BSIZE, amp->bsize, NOCRED, 0, &mapbp)) != 0) break; if (adoscksum(mapbp, amp->nwords)) { #ifdef DIAGNOSTIC printf("adosfs: loadbitmap - cksum of blk %d failed\n", adoswordn(bp, blkix)); #endif /* XXX Force read-only? Set free space 0? */ break; } n = 1; while (n < amp->nwords && mapix < bmsize) { amp->bitmap[mapix++] = bits = adoswordn(mapbp, n); ++n; if (mapix == bmsize && amp->numblks & 31) bits &= ~(0xffffffff << (amp->numblks & 31)); while (bits) { if (bits & 1) ++amp->freeblks; bits >>= 1; } } ++blkix; if (mapix < bmsize && blkix == endix) { bn = adoswordn(bp, blkix); brelse(bp, 0); if ((error = bread(amp->devvp, bn * amp->bsize / DEV_BSIZE, amp->bsize, NOCRED, 0, &bp)) != 0) break; /* * Why is there no checksum on these blocks? */ blkix = 0; endix = amp->nwords - 1; } } if (bp) brelse(bp, 0); if (mapbp) brelse(mapbp, 0); return (error); }