/* $NetBSD: msdosfs_vfsops.c,v 1.36 1995/11/29 15:08:40 ws Exp $ */ /*- * Copyright (C) 1994, 1995 Wolfgang Solfrank. * Copyright (C) 1994, 1995 TooLs GmbH. * All rights reserved. * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by TooLs GmbH. * 4. The name of TooLs GmbH may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* * Written by Paul Popelka (paulp@uts.amdahl.com) * * You can do anything you want with this software, just don't say you wrote * it, and don't remove this notice. * * This software is provided "as is". * * The author supplies this software to be publicly redistributed on the * understanding that the author is not responsible for the correct * functioning of this software in any circumstances and is not liable for * any damages caused by this software. * * October 1992 */ #include #include #include #include #include #include #include /* XXX */ /* defines v_rdev */ #include #include #include #include #include #include #include #include #include #include #include #include #include /* * mp - path - addr in user space of mount point (ie /usr or whatever) * data - addr in user space of mount params including the name of the block * special file to treat as a filesystem. */ int msdosfs_mount(mp, path, data, ndp, p) struct mount *mp; char *path; caddr_t data; struct nameidata *ndp; struct proc *p; { struct vnode *devvp; /* vnode for blk device to mount */ struct msdosfs_args args; /* will hold data from mount request */ struct msdosfsmount *pmp; /* msdosfs specific mount control block */ size_t size; int error, flags; mode_t accessmode; if (error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args))) return (error); /* * If updating, check whether changing from read-only to * read/write; if there is no device name, that's all we do. */ if (mp->mnt_flag & MNT_UPDATE) { pmp = VFSTOMSDOSFS(mp); error = 0; if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) { flags = WRITECLOSE; if (mp->mnt_flag & MNT_FORCE) flags |= FORCECLOSE; if (vfs_busy(mp)) return (EBUSY); error = vflush(mp, NULLVP, flags); vfs_unbusy(mp); } if (!error && (mp->mnt_flag & MNT_RELOAD)) /* not yet implemented */ error = EOPNOTSUPP; if (error) return (error); if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_WANTRDWR)) { /* * If upgrade to read-write by non-root, then verify * that user has necessary permissions on the device. */ if (p->p_ucred->cr_uid != 0) { devvp = pmp->pm_devvp; VOP_LOCK(devvp); if (error = VOP_ACCESS(devvp, VREAD | VWRITE, p->p_ucred, p)) { VOP_UNLOCK(devvp); return (error); } VOP_UNLOCK(devvp); } pmp->pm_flags &= ~MSDOSFSMNT_RONLY; } if (args.fspec == 0) { #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */ if (args.flags & MSDOSFSMNT_MNTOPT) { pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT; pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT; if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; } #endif /* * Process export requests. */ return (vfs_export(mp, &pmp->pm_export, &args.export)); } } /* * Not an update, or updating the name: look up the name * and verify that it refers to a sensible block device. */ NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); if (error = namei(ndp)) return (error); devvp = ndp->ni_vp; if (devvp->v_type != VBLK) { vrele(devvp); return (ENOTBLK); } if (major(devvp->v_rdev) >= nblkdev) { vrele(devvp); return (ENXIO); } /* * If mount by non-root, then verify that user has necessary * permissions on the device. */ if (p->p_ucred->cr_uid != 0) { accessmode = VREAD; if ((mp->mnt_flag & MNT_RDONLY) == 0) accessmode |= VWRITE; VOP_LOCK(devvp); if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) { vput(devvp); return (error); } VOP_UNLOCK(devvp); } if ((mp->mnt_flag & MNT_UPDATE) == 0) error = msdosfs_mountfs(devvp, mp, p); else { if (devvp != pmp->pm_devvp) error = EINVAL; /* needs translation */ else vrele(devvp); } if (error) { vrele(devvp); return (error); } pmp = VFSTOMSDOSFS(mp); pmp->pm_gid = args.gid; pmp->pm_uid = args.uid; pmp->pm_mask = args.mask; pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT; if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; else if (!(pmp->pm_flags & (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) { struct vnode *rootvp; /* * Try to divine whether to support Win'95 long filenames */ if (error = msdosfs_root(mp, &rootvp)) { msdosfs_unmount(mp, MNT_FORCE, p); return (error); } pmp->pm_flags |= findwin95(VTODE(rootvp)) ? MSDOSFSMNT_LONGNAME : MSDOSFSMNT_SHORTNAME; vput(rootvp); } (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size); bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); #ifdef MSDOSFS_DEBUG printf("msdosfs_mount(): mp %x, pmp %x, inusemap %x\n", mp, pmp, pmp->pm_inusemap); #endif return (0); } int msdosfs_mountfs(devvp, mp, p) struct vnode *devvp; struct mount *mp; struct proc *p; { struct msdosfsmount *pmp; struct buf *bp; dev_t dev = devvp->v_rdev; struct partinfo dpart; union bootsector *bsp; struct byte_bpb33 *b33; struct byte_bpb50 *b50; extern struct vnode *rootvp; u_int8_t SecPerClust; int ronly, error; #ifdef atari int bsize, dtype, tmp; #endif /* !atari */ /* * Disallow multiple mounts of the same device. * Disallow mounting of a device that is currently in use * (except for root, which might share swap device for miniroot). * Flush out any old buffers remaining from a previous use. */ if (error = vfs_mountedon(devvp)) return (error); if (vcount(devvp) > 1 && devvp != rootvp) return (EBUSY); if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) return (error); ronly = (mp->mnt_flag & MNT_RDONLY) != 0; if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p)) return (error); #ifdef atari bp = NULL; /* both used in error_exit */ pmp = NULL; /* * We need the disklabel to calculate the size of a FAT entry later on. * Also make sure the partition contains a filesystem of type FS_MSDOS. * This doesn't work for floppies, so we have to check for them too. * * At least some parts of the msdos fs driver seem to assume that the * size of a disk block will always be 512 bytes. Let's check it... */ if (error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p)) goto error_exit; tmp = dpart.part->p_fstype; dtype = dpart.disklab->d_type; bsize = dpart.disklab->d_secsize; if (bsize != 512 || (dtype != DTYPE_FLOPPY && tmp != FS_MSDOS)) { error = EINVAL; goto error_exit; } #else /* !atari */ /* * Read the boot sector of the filesystem, and then check the boot * signature. If not a dos boot sector then error out. */ bp = NULL; pmp = NULL; #endif /* !atari */ if (error = bread(devvp, 0, 512, NOCRED, &bp)) goto error_exit; bp->b_flags |= B_AGE; bsp = (union bootsector *)bp->b_data; b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; #ifdef MSDOSFS_CHECKSIG if (bsp->bs50.bsBootSectSig != BOOTSIG) { error = EINVAL; goto error_exit; } #endif pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK); bzero((caddr_t)pmp, sizeof *pmp); pmp->pm_mountp = mp; /* * Compute several useful quantities from the bpb in the * bootsector. Copy in the dos 5 variant of the bpb then fix up * the fields that are different between dos 5 and dos 3.3. */ SecPerClust = b50->bpbSecPerClust; pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); pmp->pm_ResSectors = getushort(b50->bpbResSectors); pmp->pm_FATs = b50->bpbFATs; pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); pmp->pm_Sectors = getushort(b50->bpbSectors); pmp->pm_FATsecs = getushort(b50->bpbFATsecs); #ifdef atari /* * Meaningless on a gemdos fs. This kind of information * should be extracted from the disklabel structure. */ pmp->pm_SecPerTrack = 1; /* anything between 1 and 63 */ pmp->pm_Heads = 1; /* anything between 1 and 255 */ pmp->pm_Media = 0; /* unused, any value will do */ #else /* !atari */ pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); pmp->pm_Heads = getushort(b50->bpbHeads); pmp->pm_Media = b50->bpbMedia; /* XXX - We should probably check more values here */ if (!pmp->pm_BytesPerSec || !SecPerClust || pmp->pm_Heads > 255 || pmp->pm_SecPerTrack > 63) { error = EINVAL; goto error_exit; } #endif /* !atari */ if (pmp->pm_Sectors == 0) { pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); } else { pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); pmp->pm_HugeSectors = pmp->pm_Sectors; } #ifdef atari /* * Check a few values (could do some more): * - logical sector size: power of 2, >= block size * - sectors per cluster: power of 2, >= 1 * - number of sectors: >= 1, <= size of partition */ if ( pmp->pm_BytesPerSec < bsize || pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1) || !SecPerClust || SecPerClust & (SecPerClust - 1) || !pmp->pm_HugeSectors || pmp->pm_HugeSectors * pmp->pm_BytesPerSec > dpart.part->p_size * bsize) { error = EINVAL; goto error_exit; } /* * XXX - Many parts of the msdos fs driver seem to assume that * the number of bytes per logical sector (BytesPerSec) will * always be the same as the number of bytes per disk block * Let's pretend it is. */ tmp = pmp->pm_BytesPerSec / bsize; pmp->pm_BytesPerSec = bsize; pmp->pm_HugeSectors *= tmp; pmp->pm_HiddenSects *= tmp; pmp->pm_ResSectors *= tmp; pmp->pm_Sectors *= tmp; pmp->pm_FATsecs *= tmp; #endif /* atari */ pmp->pm_fatblk = pmp->pm_ResSectors; pmp->pm_rootdirblk = pmp->pm_fatblk + (pmp->pm_FATs * pmp->pm_FATsecs); pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) + pmp->pm_BytesPerSec - 1) / pmp->pm_BytesPerSec;/* in sectors */ pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / SecPerClust; pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1; pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec; #ifdef atari if (dtype == DTYPE_FLOPPY) { pmp->pm_fatentrysize = 12; pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec; } else { pmp->pm_fatentrysize = 16; pmp->pm_fatblocksize = MAXBSIZE; } #else /* !atari */ if (FAT12(pmp)) /* * This will usually be a floppy disk. This size makes sure * that one fat entry will not be split across multiple * blocks. */ pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec; else /* * This will usually be a hard disk. Reading or writing one * block should be quite fast. */ pmp->pm_fatblocksize = MAXBSIZE; #endif /* !atari */ pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec; pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1; /* * Compute mask and shift value for isolating cluster relative byte * offsets and cluster numbers from a file offset. */ pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec; pmp->pm_crbomask = pmp->pm_bpcluster - 1; pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; #ifdef atari /* * We already know that the number of sectors per cluster is * > 0 and a power of 2. */ #else /* !atari */ /* * Check for valid cluster size * must be a power of 2 */ if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { error = EINVAL; goto error_exit; } #endif /* !atari */ /* * Release the bootsector buffer. */ brelse(bp); bp = NULL; /* * Allocate memory for the bitmap of allocated clusters, and then * fill it in. */ pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1) / N_INUSEBITS) * sizeof(*pmp->pm_inusemap), M_MSDOSFSFAT, M_WAITOK); /* * fillinusemap() needs pm_devvp. */ pmp->pm_dev = dev; pmp->pm_devvp = devvp; /* * Have the inuse map filled in. */ if (error = fillinusemap(pmp)) goto error_exit; /* * If they want fat updates to be synchronous then let them suffer * the performance degradation in exchange for the on disk copy of * the fat being correct just about all the time. I suppose this * would be a good thing to turn on if the kernel is still flakey. */ if (mp->mnt_flag & MNT_SYNCHRONOUS) pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; /* * Finish up. */ if (ronly) pmp->pm_flags |= MSDOSFSMNT_RONLY; else pmp->pm_fmod = 1; mp->mnt_data = (qaddr_t)pmp; mp->mnt_stat.f_fsid.val[0] = (long)dev; mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_MSDOS); mp->mnt_flag |= MNT_LOCAL; #ifdef QUOTA /* * If we ever do quotas for DOS filesystems this would be a place * to fill in the info in the msdosfsmount structure. You dolt, * quotas on dos filesystems make no sense because files have no * owners on dos filesystems. of course there is some empty space * in the directory entry where we could put uid's and gid's. */ #endif devvp->v_specflags |= SI_MOUNTEDON; return (0); error_exit:; if (bp) brelse(bp); (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); if (pmp) { if (pmp->pm_inusemap) free(pmp->pm_inusemap, M_MSDOSFSFAT); free(pmp, M_MSDOSFSMNT); mp->mnt_data = (qaddr_t)0; } return (error); } int msdosfs_start(mp, flags, p) struct mount *mp; int flags; struct proc *p; { return (0); } /* * Unmount the filesystem described by mp. */ int msdosfs_unmount(mp, mntflags, p) struct mount *mp; int mntflags; struct proc *p; { struct msdosfsmount *pmp; int error, flags; flags = 0; if (mntflags & MNT_FORCE) flags |= FORCECLOSE; #ifdef QUOTA #endif if (error = vflush(mp, NULLVP, flags)) return (error); pmp = VFSTOMSDOSFS(mp); pmp->pm_devvp->v_specflags &= ~SI_MOUNTEDON; #ifdef MSDOSFS_DEBUG printf("msdosfs_umount(): just before calling VOP_CLOSE()\n"); printf("flag %08x, usecount %d, writecount %d, holdcnt %d\n", vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt); printf("lastr %d, id %d, mount %08x, op %08x\n", vp->v_lastr, vp->v_id, vp->v_mount, vp->v_op); printf("freef %08x, freeb %08x, mountf %08x, mountb %08x\n", vp->v_freef, vp->v_freeb, vp->v_mountf, vp->v_mountb); printf("cleanblkhd %08x, dirtyblkhd %08x, numoutput %d, type %d\n", vp->v_cleanblkhd, vp->v_dirtyblkhd, vp->v_numoutput, vp->v_type); printf("union %08x, tag %d, data[0] %08x, data[1] %08x\n", vp->v_socket, vp->v_tag, vp->v_data[0], vp->v_data[1]); #endif error = VOP_CLOSE(pmp->pm_devvp, pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, p); vrele(pmp->pm_devvp); free(pmp->pm_inusemap, M_MSDOSFSFAT); free(pmp, M_MSDOSFSMNT); mp->mnt_data = (qaddr_t)0; mp->mnt_flag &= ~MNT_LOCAL; return (error); } int msdosfs_root(mp, vpp) struct mount *mp; struct vnode **vpp; { struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); struct denode *ndep; int error; #ifdef MSDOSFS_DEBUG printf("msdosfs_root(); mp %08x, pmp %08x, ndep %08x, vp %08x\n", mp, pmp, ndep, DETOV(ndep)); #endif if (error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) return (error); *vpp = DETOV(ndep); return (0); } int msdosfs_quotactl(mp, cmds, uid, arg, p) struct mount *mp; int cmds; uid_t uid; caddr_t arg; struct proc *p; { #ifdef QUOTA return (EOPNOTSUPP); #else return (EOPNOTSUPP); #endif } int msdosfs_statfs(mp, sbp, p) struct mount *mp; struct statfs *sbp; struct proc *p; { struct msdosfsmount *pmp; pmp = VFSTOMSDOSFS(mp); #ifdef COMPAT_09 sbp->f_type = 4; #else sbp->f_type = 0; #endif sbp->f_bsize = pmp->pm_bpcluster; sbp->f_iosize = pmp->pm_bpcluster; sbp->f_blocks = pmp->pm_nmbrofclusters; sbp->f_bfree = pmp->pm_freeclustercount; sbp->f_bavail = pmp->pm_freeclustercount; sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ sbp->f_ffree = 0; /* what to put in here? */ if (sbp != &mp->mnt_stat) { bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); } strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN); return (0); } int msdosfs_sync(mp, waitfor, cred, p) struct mount *mp; int waitfor; struct ucred *cred; struct proc *p; { struct vnode *vp; struct denode *dep; struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); int error, allerror = 0; /* * If we ever switch to not updating all of the fats all the time, * this would be the place to update them from the first one. */ if (pmp->pm_fmod != 0) if (pmp->pm_flags & MSDOSFSMNT_RONLY) panic("msdosfs_sync: rofs mod"); else { /* update fats here */ } /* * Write back each (modified) denode. */ loop: for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = vp->v_mntvnodes.le_next) { /* * If the vnode that we are about to sync is no longer * assoicated with this mount point, start over. */ if (vp->v_mount != mp) goto loop; if (VOP_ISLOCKED(vp)) continue; dep = VTODE(vp); if (dep->de_flag & (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED) == 0 && vp->v_dirtyblkhd.lh_first == NULL) continue; if (vget(vp, 1)) goto loop; if (error = VOP_FSYNC(vp, cred, waitfor, p)) allerror = error; vput(vp); } /* * Force stale file system control information to be flushed. */ if (error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p)) allerror = error; #ifdef QUOTA #endif return (allerror); } int msdosfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) struct mount *mp; struct fid *fhp; struct mbuf *nam; struct vnode **vpp; int *exflagsp; struct ucred **credanonp; { struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); struct defid *defhp = (struct defid *) fhp; struct denode *dep; struct netcred *np; int error; np = vfs_export_lookup(mp, &pmp->pm_export, nam); if (np == NULL) return (EACCES); error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); if (error) { *vpp = NULLVP; return (error); } *vpp = DETOV(dep); *exflagsp = np->netc_exflags; *credanonp = &np->netc_anon; return (0); } int msdosfs_vptofh(vp, fhp) struct vnode *vp; struct fid *fhp; { struct denode *dep; struct defid *defhp; dep = VTODE(vp); defhp = (struct defid *)fhp; defhp->defid_len = sizeof(struct defid); defhp->defid_dirclust = dep->de_dirclust; defhp->defid_dirofs = dep->de_diroffset; /* defhp->defid_gen = dep->de_gen; */ return (0); } int msdosfs_vget(mp, ino, vpp) struct mount *mp; ino_t ino; struct vnode **vpp; { return (EOPNOTSUPP); } struct vfsops msdosfs_vfsops = { MOUNT_MSDOS, msdosfs_mount, msdosfs_start, msdosfs_unmount, msdosfs_root, msdosfs_quotactl, msdosfs_statfs, msdosfs_sync, msdosfs_vget, msdosfs_fhtovp, msdosfs_vptofh, msdosfs_init };