/* * 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); }
/* * 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); }
/* * lookup an anode, check mount's hash table if not found, create * return locked and referenced al la vget(vp, 1); */ int adosfs_vget(struct mount *mp, ino_t an, struct vnode **vpp) { struct adosfsmount *amp; struct vnode *vp; struct anode *ap; struct buf *bp; char *nam, *tmp; int namlen, error; error = 0; amp = VFSTOADOSFS(mp); bp = NULL; /* * check hash table. we are done if found */ if ((*vpp = adosfs_ahashget(mp, an)) != NULL) return (0); error = getnewvnode(VT_ADOSFS, mp, adosfs_vnodeop_p, NULL, &vp); if (error) return (error); /* * setup, insert in hash, and lock before io. */ vp->v_data = ap = pool_get(&adosfs_node_pool, PR_WAITOK); memset(ap, 0, sizeof(struct anode)); ap->vp = vp; ap->amp = amp; ap->block = an; ap->nwords = amp->nwords; genfs_node_init(vp, &adosfs_genfsops); adosfs_ainshash(amp, ap); if ((error = bread(amp->devvp, an * amp->bsize / DEV_BSIZE, amp->bsize, NOCRED, 0, &bp)) != 0) { vput(vp); return (error); } /* * get type and fill rest in based on that. */ switch (ap->type = adosfs_getblktype(amp, bp)) { case AROOT: vp->v_type = VDIR; vp->v_vflag |= VV_ROOT; ap->mtimev.days = adoswordn(bp, ap->nwords - 10); ap->mtimev.mins = adoswordn(bp, ap->nwords - 9); ap->mtimev.ticks = adoswordn(bp, ap->nwords - 8); ap->created.days = adoswordn(bp, ap->nwords - 7); ap->created.mins = adoswordn(bp, ap->nwords - 6); ap->created.ticks = adoswordn(bp, ap->nwords - 5); break; case ALDIR: case ADIR: vp->v_type = VDIR; break; case ALFILE: case AFILE: vp->v_type = VREG; ap->fsize = adoswordn(bp, ap->nwords - 47); break; case ASLINK: /* XXX soft link */ vp->v_type = VLNK; /* * convert from BCPL string and * from: "part:dir/file" to: "/part/dir/file" */ nam = (char *)bp->b_data + (6 * sizeof(long)); namlen = strlen(nam); tmp = nam; while (*tmp && *tmp != ':') tmp++; if (*tmp == 0) { ap->slinkto = malloc(namlen + 1, M_ANODE, M_WAITOK); memcpy(ap->slinkto, nam, namlen); } else if (*nam == ':') { ap->slinkto = malloc(namlen + 1, M_ANODE, M_WAITOK); memcpy(ap->slinkto, nam, namlen); ap->slinkto[0] = '/'; } else { ap->slinkto = malloc(namlen + 2, M_ANODE, M_WAITOK); ap->slinkto[0] = '/'; memcpy(&ap->slinkto[1], nam, namlen); ap->slinkto[tmp - nam + 1] = '/'; namlen++; } ap->slinkto[namlen] = 0; ap->fsize = namlen; break; default: brelse(bp, 0); vput(vp); return (EINVAL); } /* * Get appropriate data from this block; hard link needs * to get other data from the "real" block. */ /* * copy in name (from original block) */ nam = (char *)bp->b_data + (ap->nwords - 20) * sizeof(u_int32_t); namlen = *(u_char *)nam++; if (namlen > 30) { #ifdef DIAGNOSTIC printf("adosfs: aget: name length too long blk %llu\n", (unsigned long long)an); #endif brelse(bp, 0); vput(vp); return (EINVAL); } memcpy(ap->name, nam, namlen); ap->name[namlen] = 0; /* * if dir alloc hash table and copy it in */ if (vp->v_type == VDIR) { int i; ap->tab = malloc(ANODETABSZ(ap) * 2, M_ANODE, M_WAITOK); ap->ntabent = ANODETABENT(ap); ap->tabi = (int *)&ap->tab[ap->ntabent]; memset(ap->tabi, 0, ANODETABSZ(ap)); for (i = 0; i < ap->ntabent; i++) ap->tab[i] = adoswordn(bp, i + 6); } /* * misc. */ ap->pblock = adoswordn(bp, ap->nwords - 3); ap->hashf = adoswordn(bp, ap->nwords - 4); ap->linknext = adoswordn(bp, ap->nwords - 10); ap->linkto = adoswordn(bp, ap->nwords - 11); /* * setup last indirect block cache. */ ap->lastlindblk = 0; if (ap->type == AFILE) { ap->lastindblk = ap->block; if (adoswordn(bp, ap->nwords - 10)) ap->linkto = ap->block; } else if (ap->type == ALFILE) { ap->lastindblk = ap->linkto; brelse(bp, 0); bp = NULL; error = bread(amp->devvp, ap->linkto * amp->bsize / DEV_BSIZE, amp->bsize, NOCRED, 0, &bp); if (error) { vput(vp); return (error); } ap->fsize = adoswordn(bp, ap->nwords - 47); /* * Should ap->block be set to the real file header block? */ ap->block = ap->linkto; } if (ap->type == AROOT) { ap->adprot = 15; ap->uid = amp->uid; ap->gid = amp->gid; } else { ap->adprot = adoswordn(bp, ap->nwords - 48) ^ 15; /* * ADOS directories do not have a `x' protection bit as * it is known in VFS; this functionality is fulfilled * by the ADOS `r' bit. * * To retain the ADOS behaviour, fake execute permissions * in that case. */ if ((ap->type == ADIR || ap->type == ALDIR) && (ap->adprot & 0x00000008) == 0) ap->adprot &= ~0x00000002; /* * Get uid/gid from extensions in file header * (really need to know if this is a muFS partition) */ ap->uid = (adoswordn(bp, ap->nwords - 49) >> 16) & 0xffff; ap->gid = adoswordn(bp, ap->nwords - 49) & 0xffff; if (ap->uid || ap->gid) { if (ap->uid == 0xffff) ap->uid = 0; if (ap->gid == 0xffff) ap->gid = 0; ap->adprot |= 0x40000000; /* Kludge */ } else { /* * uid & gid extension don't exist, * so use the mount-point uid/gid */ ap->uid = amp->uid; ap->gid = amp->gid; } } ap->mtime.days = adoswordn(bp, ap->nwords - 23); ap->mtime.mins = adoswordn(bp, ap->nwords - 22); ap->mtime.ticks = adoswordn(bp, ap->nwords - 21); *vpp = vp; brelse(bp, 0); uvm_vnp_setsize(vp, ap->fsize); return (0); }
/* * 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); }
int adosfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l) { struct disklabel dl; struct partition *parp; struct adosfsmount *amp; struct buf *bp; struct vnode *rvp; size_t bitmap_sz = 0; int error, i; uint64_t numsecs; unsigned secsize; unsigned long secsperblk, blksperdisk, resvblks; amp = NULL; if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0) return (error); /* * open blkdev and read boot and root block */ vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); if ((error = VOP_OPEN(devvp, FREAD, NOCRED)) != 0) { VOP_UNLOCK(devvp); return (error); } error = getdisksize(devvp, &numsecs, &secsize); if (error) goto fail; amp = kmem_zalloc(sizeof(struct adosfsmount), KM_SLEEP); /* * compute filesystem parameters from disklabel * on arch/amiga the disklabel is computed from the native * partition tables * - p_fsize is the filesystem block size * - p_frag is the number of sectors per filesystem block * - p_cpg is the number of reserved blocks (boot blocks) * - p_psize is reduced by the number of preallocated blocks * at the end of a partition * * XXX * - bsize and secsperblk could be computed from the first sector * of the root block * - resvblks (the number of boot blocks) can only be guessed * by scanning for the root block as its position moves * with resvblks */ error = VOP_IOCTL(devvp, DIOCGDINFO, &dl, FREAD, NOCRED); VOP_UNLOCK(devvp); if (error) goto fail; parp = &dl.d_partitions[DISKPART(devvp->v_rdev)]; if (dl.d_type == DTYPE_FLOPPY) { amp->bsize = secsize; secsperblk = 1; resvblks = 2; } else if (parp->p_fsize > 0 && parp->p_frag > 0) { amp->bsize = parp->p_fsize * parp->p_frag; secsperblk = parp->p_frag; resvblks = parp->p_cpg; } else { error = EINVAL; goto fail; } blksperdisk = numsecs / secsperblk; /* The filesytem variant ('dostype') is stored in the boot block */ bp = NULL; if ((error = bread(devvp, (daddr_t)BBOFF, amp->bsize, NOCRED, 0, &bp)) != 0) { goto fail; } amp->dostype = adoswordn(bp, 0); brelse(bp, 0); /* basic sanity checks */ if (amp->dostype < 0x444f5300 || amp->dostype > 0x444f5305) { error = EINVAL; goto fail; } amp->rootb = (blksperdisk - 1 + resvblks) / 2; amp->numblks = blksperdisk - resvblks; amp->nwords = amp->bsize >> 2; amp->dbsize = amp->bsize - (IS_FFS(amp) ? 0 : OFS_DATA_OFFSET); amp->devvp = devvp; amp->mp = mp; mp->mnt_data = amp; mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev; mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_ADOSFS); mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; mp->mnt_stat.f_namemax = ADMAXNAMELEN; mp->mnt_fs_bshift = ffs(amp->bsize) - 1; mp->mnt_dev_bshift = DEV_BSHIFT; mp->mnt_flag |= MNT_LOCAL; /* * init anode table. */ for (i = 0; i < ANODEHASHSZ; i++) LIST_INIT(&->anodetab[i]); /* * get the root anode, if not a valid fs this will fail. */ if ((error = VFS_ROOT(mp, &rvp)) != 0) goto fail; /* allocate and load bitmap, set free space */ bitmap_sz = ((amp->numblks + 31) / 32) * sizeof(*amp->bitmap); amp->bitmap = kmem_alloc(bitmap_sz, KM_SLEEP); if (amp->bitmap) adosfs_loadbitmap(amp); if (mp->mnt_flag & MNT_RDONLY && amp->bitmap) { /* * Don't need the bitmap any more if it's read-only. */ kmem_free(amp->bitmap, bitmap_sz); amp->bitmap = NULL; } vput(rvp); return(0); fail: vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); (void) VOP_CLOSE(devvp, FREAD, NOCRED); VOP_UNLOCK(devvp); if (amp && amp->bitmap) kmem_free(amp->bitmap, bitmap_sz); if (amp) kmem_free(amp, sizeof(*amp)); return (error); }