1c3aac50fSPeter Wemm /* $FreeBSD$ */ 2952a6212SJordan K. Hubbard /* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */ 327a0bc89SDoug Rabson 427a0bc89SDoug Rabson /*- 5952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 6952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 727a0bc89SDoug Rabson * All rights reserved. 827a0bc89SDoug Rabson * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 927a0bc89SDoug Rabson * 1027a0bc89SDoug Rabson * Redistribution and use in source and binary forms, with or without 1127a0bc89SDoug Rabson * modification, are permitted provided that the following conditions 1227a0bc89SDoug Rabson * are met: 1327a0bc89SDoug Rabson * 1. Redistributions of source code must retain the above copyright 1427a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer. 1527a0bc89SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 1627a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer in the 1727a0bc89SDoug Rabson * documentation and/or other materials provided with the distribution. 1827a0bc89SDoug Rabson * 3. All advertising materials mentioning features or use of this software 1927a0bc89SDoug Rabson * must display the following acknowledgement: 2027a0bc89SDoug Rabson * This product includes software developed by TooLs GmbH. 2127a0bc89SDoug Rabson * 4. The name of TooLs GmbH may not be used to endorse or promote products 2227a0bc89SDoug Rabson * derived from this software without specific prior written permission. 2327a0bc89SDoug Rabson * 2427a0bc89SDoug Rabson * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2527a0bc89SDoug Rabson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2627a0bc89SDoug Rabson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2727a0bc89SDoug Rabson * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2827a0bc89SDoug Rabson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2927a0bc89SDoug Rabson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3027a0bc89SDoug Rabson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3127a0bc89SDoug Rabson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3227a0bc89SDoug Rabson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3327a0bc89SDoug Rabson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3427a0bc89SDoug Rabson */ 3527a0bc89SDoug Rabson /* 3627a0bc89SDoug Rabson * Written by Paul Popelka (paulp@uts.amdahl.com) 3727a0bc89SDoug Rabson * 3827a0bc89SDoug Rabson * You can do anything you want with this software, just don't say you wrote 3927a0bc89SDoug Rabson * it, and don't remove this notice. 4027a0bc89SDoug Rabson * 4127a0bc89SDoug Rabson * This software is provided "as is". 4227a0bc89SDoug Rabson * 4327a0bc89SDoug Rabson * The author supplies this software to be publicly redistributed on the 4427a0bc89SDoug Rabson * understanding that the author is not responsible for the correct 4527a0bc89SDoug Rabson * functioning of this software in any circumstances and is not liable for 4627a0bc89SDoug Rabson * any damages caused by this software. 4727a0bc89SDoug Rabson * 4827a0bc89SDoug Rabson * October 1992 4927a0bc89SDoug Rabson */ 5027a0bc89SDoug Rabson 5127a0bc89SDoug Rabson #include <sys/param.h> 5227a0bc89SDoug Rabson #include <sys/systm.h> 5365baf8f0SBruce Evans #include <sys/conf.h> 5427a0bc89SDoug Rabson #include <sys/namei.h> 5527a0bc89SDoug Rabson #include <sys/proc.h> 5627a0bc89SDoug Rabson #include <sys/kernel.h> 5727a0bc89SDoug Rabson #include <sys/vnode.h> 5827a0bc89SDoug Rabson #include <sys/mount.h> 5927a0bc89SDoug Rabson #include <sys/buf.h> 603ac4d1efSBruce Evans #include <sys/fcntl.h> 6127a0bc89SDoug Rabson #include <sys/malloc.h> 62952a6212SJordan K. Hubbard #include <sys/stat.h> /* defines ALLPERMS */ 6327a0bc89SDoug Rabson 6427a0bc89SDoug Rabson #include <msdosfs/bpb.h> 6527a0bc89SDoug Rabson #include <msdosfs/bootsect.h> 6627a0bc89SDoug Rabson #include <msdosfs/direntry.h> 6727a0bc89SDoug Rabson #include <msdosfs/denode.h> 6827a0bc89SDoug Rabson #include <msdosfs/msdosfsmount.h> 6927a0bc89SDoug Rabson #include <msdosfs/fat.h> 7027a0bc89SDoug Rabson 71a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure"); 72a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table"); 7355166637SPoul-Henning Kamp 74952a6212SJordan K. Hubbard static int update_mp __P((struct mount *mp, struct msdosfs_args *argp)); 75af482601SBruce Evans static int mountmsdosfs __P((struct vnode *devvp, struct mount *mp, 76952a6212SJordan K. Hubbard struct proc *p, struct msdosfs_args *argp)); 77af482601SBruce Evans static int msdosfs_fhtovp __P((struct mount *, struct fid *, 78c24fda81SAlfred Perlstein struct vnode **)); 79c24fda81SAlfred Perlstein static int msdosfs_checkexp __P((struct mount *, struct sockaddr *, 80c24fda81SAlfred Perlstein int *, struct ucred **)); 81af482601SBruce Evans static int msdosfs_mount __P((struct mount *, char *, caddr_t, 82af482601SBruce Evans struct nameidata *, struct proc *)); 83af482601SBruce Evans static int msdosfs_root __P((struct mount *, struct vnode **)); 84af482601SBruce Evans static int msdosfs_statfs __P((struct mount *, struct statfs *, 85af482601SBruce Evans struct proc *)); 86af482601SBruce Evans static int msdosfs_sync __P((struct mount *, int, struct ucred *, 87af482601SBruce Evans struct proc *)); 88af482601SBruce Evans static int msdosfs_unmount __P((struct mount *, int, struct proc *)); 89af482601SBruce Evans static int msdosfs_vptofh __P((struct vnode *, struct fid *)); 90af482601SBruce Evans 91952a6212SJordan K. Hubbard static int 92952a6212SJordan K. Hubbard update_mp(mp, argp) 93952a6212SJordan K. Hubbard struct mount *mp; 94952a6212SJordan K. Hubbard struct msdosfs_args *argp; 95952a6212SJordan K. Hubbard { 96952a6212SJordan K. Hubbard struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 97952a6212SJordan K. Hubbard int error; 98952a6212SJordan K. Hubbard 99952a6212SJordan K. Hubbard pmp->pm_gid = argp->gid; 100952a6212SJordan K. Hubbard pmp->pm_uid = argp->uid; 101952a6212SJordan K. Hubbard pmp->pm_mask = argp->mask & ALLPERMS; 102952a6212SJordan K. Hubbard pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT; 1037391f611SAndrey A. Chernov if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) { 10413df76f2SAndrey A. Chernov bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w)); 1057391f611SAndrey A. Chernov bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u)); 1067391f611SAndrey A. Chernov bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d)); 1077391f611SAndrey A. Chernov } 1087391f611SAndrey A. Chernov if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) { 10964fb806cSAndrey A. Chernov bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul)); 1107391f611SAndrey A. Chernov bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu)); 1117391f611SAndrey A. Chernov } 112952a6212SJordan K. Hubbard 113952a6212SJordan K. Hubbard #ifndef __FreeBSD__ 114952a6212SJordan K. Hubbard /* 115952a6212SJordan K. Hubbard * GEMDOS knows nothing (yet) about win95 116952a6212SJordan K. Hubbard */ 117952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS) 118952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFSMNT_NOWIN95; 119952a6212SJordan K. Hubbard #endif 120952a6212SJordan K. Hubbard 121952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 122952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 123952a6212SJordan K. Hubbard else if (!(pmp->pm_flags & 124952a6212SJordan K. Hubbard (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) { 125952a6212SJordan K. Hubbard struct vnode *rootvp; 126952a6212SJordan K. Hubbard 127952a6212SJordan K. Hubbard /* 128952a6212SJordan K. Hubbard * Try to divine whether to support Win'95 long filenames 129952a6212SJordan K. Hubbard */ 130952a6212SJordan K. Hubbard if (FAT32(pmp)) 131952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFSMNT_LONGNAME; 132952a6212SJordan K. Hubbard else { 133952a6212SJordan K. Hubbard if ((error = msdosfs_root(mp, &rootvp)) != 0) 134952a6212SJordan K. Hubbard return error; 135952a6212SJordan K. Hubbard pmp->pm_flags |= findwin95(VTODE(rootvp)) 136952a6212SJordan K. Hubbard ? MSDOSFSMNT_LONGNAME 137952a6212SJordan K. Hubbard : MSDOSFSMNT_SHORTNAME; 138952a6212SJordan K. Hubbard vput(rootvp); 139952a6212SJordan K. Hubbard } 140952a6212SJordan K. Hubbard } 141952a6212SJordan K. Hubbard return 0; 142952a6212SJordan K. Hubbard } 143952a6212SJordan K. Hubbard 144952a6212SJordan K. Hubbard #ifndef __FreeBSD__ 145952a6212SJordan K. Hubbard int 146952a6212SJordan K. Hubbard msdosfs_mountroot() 147952a6212SJordan K. Hubbard { 148952a6212SJordan K. Hubbard register struct mount *mp; 149952a6212SJordan K. Hubbard struct proc *p = curproc; /* XXX */ 150952a6212SJordan K. Hubbard size_t size; 151952a6212SJordan K. Hubbard int error; 152952a6212SJordan K. Hubbard struct msdosfs_args args; 153952a6212SJordan K. Hubbard 154952a6212SJordan K. Hubbard if (root_device->dv_class != DV_DISK) 155952a6212SJordan K. Hubbard return (ENODEV); 156952a6212SJordan K. Hubbard 157952a6212SJordan K. Hubbard /* 158952a6212SJordan K. Hubbard * Get vnodes for swapdev and rootdev. 159952a6212SJordan K. Hubbard */ 160952a6212SJordan K. Hubbard if (bdevvp(rootdev, &rootvp)) 161952a6212SJordan K. Hubbard panic("msdosfs_mountroot: can't setup rootvp"); 162952a6212SJordan K. Hubbard 163952a6212SJordan K. Hubbard mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 164952a6212SJordan K. Hubbard bzero((char *)mp, (u_long)sizeof(struct mount)); 165952a6212SJordan K. Hubbard mp->mnt_op = &msdosfs_vfsops; 166952a6212SJordan K. Hubbard mp->mnt_flag = 0; 167952a6212SJordan K. Hubbard LIST_INIT(&mp->mnt_vnodelist); 168952a6212SJordan K. Hubbard 169952a6212SJordan K. Hubbard args.flags = 0; 170952a6212SJordan K. Hubbard args.uid = 0; 171952a6212SJordan K. Hubbard args.gid = 0; 172952a6212SJordan K. Hubbard args.mask = 0777; 173952a6212SJordan K. Hubbard 174952a6212SJordan K. Hubbard if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) { 175952a6212SJordan K. Hubbard free(mp, M_MOUNT); 176952a6212SJordan K. Hubbard return (error); 177952a6212SJordan K. Hubbard } 178952a6212SJordan K. Hubbard 179952a6212SJordan K. Hubbard if ((error = update_mp(mp, &args)) != 0) { 180952a6212SJordan K. Hubbard (void)msdosfs_unmount(mp, 0, p); 181952a6212SJordan K. Hubbard free(mp, M_MOUNT); 182952a6212SJordan K. Hubbard return (error); 183952a6212SJordan K. Hubbard } 184952a6212SJordan K. Hubbard 185952a6212SJordan K. Hubbard if ((error = vfs_lock(mp)) != 0) { 186952a6212SJordan K. Hubbard (void)msdosfs_unmount(mp, 0, p); 187952a6212SJordan K. Hubbard free(mp, M_MOUNT); 188952a6212SJordan K. Hubbard return (error); 189952a6212SJordan K. Hubbard } 190952a6212SJordan K. Hubbard 1910429e37aSPoul-Henning Kamp TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 192952a6212SJordan K. Hubbard mp->mnt_vnodecovered = NULLVP; 193952a6212SJordan K. Hubbard (void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1, 194952a6212SJordan K. Hubbard &size); 195952a6212SJordan K. Hubbard bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 196952a6212SJordan K. Hubbard (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 197952a6212SJordan K. Hubbard &size); 198952a6212SJordan K. Hubbard bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 199952a6212SJordan K. Hubbard (void)msdosfs_statfs(mp, &mp->mnt_stat, p); 200952a6212SJordan K. Hubbard vfs_unlock(mp); 201952a6212SJordan K. Hubbard return (0); 202952a6212SJordan K. Hubbard } 203952a6212SJordan K. Hubbard #endif 204952a6212SJordan K. Hubbard 20527a0bc89SDoug Rabson /* 20627a0bc89SDoug Rabson * mp - path - addr in user space of mount point (ie /usr or whatever) 20727a0bc89SDoug Rabson * data - addr in user space of mount params including the name of the block 20827a0bc89SDoug Rabson * special file to treat as a filesystem. 20927a0bc89SDoug Rabson */ 2107fefffeeSPoul-Henning Kamp static int 21127a0bc89SDoug Rabson msdosfs_mount(mp, path, data, ndp, p) 21227a0bc89SDoug Rabson struct mount *mp; 21327a0bc89SDoug Rabson char *path; 21427a0bc89SDoug Rabson caddr_t data; 21527a0bc89SDoug Rabson struct nameidata *ndp; 21627a0bc89SDoug Rabson struct proc *p; 21727a0bc89SDoug Rabson { 21827a0bc89SDoug Rabson struct vnode *devvp; /* vnode for blk device to mount */ 21927a0bc89SDoug Rabson struct msdosfs_args args; /* will hold data from mount request */ 220952a6212SJordan K. Hubbard /* msdosfs specific mount control block */ 221952a6212SJordan K. Hubbard struct msdosfsmount *pmp = NULL; 222952a6212SJordan K. Hubbard size_t size; 22327a0bc89SDoug Rabson int error, flags; 224952a6212SJordan K. Hubbard mode_t accessmode; 22527a0bc89SDoug Rabson 226c3c6d51eSPoul-Henning Kamp error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args)); 227c3c6d51eSPoul-Henning Kamp if (error) 228952a6212SJordan K. Hubbard return (error); 229206faeeeSDmitrij Tejblum if (args.magic != MSDOSFS_ARGSMAGIC) 230952a6212SJordan K. Hubbard args.flags = 0; 23127a0bc89SDoug Rabson /* 232952a6212SJordan K. Hubbard * If updating, check whether changing from read-only to 233952a6212SJordan K. Hubbard * read/write; if there is no device name, that's all we do. 23427a0bc89SDoug Rabson */ 23527a0bc89SDoug Rabson if (mp->mnt_flag & MNT_UPDATE) { 236952a6212SJordan K. Hubbard pmp = VFSTOMSDOSFS(mp); 23727a0bc89SDoug Rabson error = 0; 238952a6212SJordan K. Hubbard if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) { 23927a0bc89SDoug Rabson flags = WRITECLOSE; 24027a0bc89SDoug Rabson if (mp->mnt_flag & MNT_FORCE) 24127a0bc89SDoug Rabson flags |= FORCECLOSE; 24227a0bc89SDoug Rabson error = vflush(mp, NULLVP, flags); 24327a0bc89SDoug Rabson } 24427a0bc89SDoug Rabson if (!error && (mp->mnt_flag & MNT_RELOAD)) 24527a0bc89SDoug Rabson /* not yet implemented */ 246952a6212SJordan K. Hubbard error = EOPNOTSUPP; 24727a0bc89SDoug Rabson if (error) 248952a6212SJordan K. Hubbard return (error); 249952a6212SJordan K. Hubbard if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) { 250952a6212SJordan K. Hubbard /* 251952a6212SJordan K. Hubbard * If upgrade to read-write by non-root, then verify 252952a6212SJordan K. Hubbard * that user has necessary permissions on the device. 253952a6212SJordan K. Hubbard */ 254952a6212SJordan K. Hubbard if (p->p_ucred->cr_uid != 0) { 255952a6212SJordan K. Hubbard devvp = pmp->pm_devvp; 256206faeeeSDmitrij Tejblum vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 257952a6212SJordan K. Hubbard error = VOP_ACCESS(devvp, VREAD | VWRITE, 258952a6212SJordan K. Hubbard p->p_ucred, p); 259952a6212SJordan K. Hubbard if (error) { 260952a6212SJordan K. Hubbard VOP_UNLOCK(devvp, 0, p); 261952a6212SJordan K. Hubbard return (error); 262952a6212SJordan K. Hubbard } 263952a6212SJordan K. Hubbard VOP_UNLOCK(devvp, 0, p); 264952a6212SJordan K. Hubbard } 265952a6212SJordan K. Hubbard pmp->pm_flags &= ~MSDOSFSMNT_RONLY; 266952a6212SJordan K. Hubbard } 26727a0bc89SDoug Rabson if (args.fspec == 0) { 268952a6212SJordan K. Hubbard #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */ 269952a6212SJordan K. Hubbard if (args.flags & MSDOSFSMNT_MNTOPT) { 270952a6212SJordan K. Hubbard pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT; 271952a6212SJordan K. Hubbard pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT; 272952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 273952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 274952a6212SJordan K. Hubbard } 275952a6212SJordan K. Hubbard #endif 27627a0bc89SDoug Rabson /* 27727a0bc89SDoug Rabson * Process export requests. 27827a0bc89SDoug Rabson */ 279952a6212SJordan K. Hubbard return (vfs_export(mp, &pmp->pm_export, &args.export)); 28027a0bc89SDoug Rabson } 281952a6212SJordan K. Hubbard } 28227a0bc89SDoug Rabson /* 283952a6212SJordan K. Hubbard * Not an update, or updating the name: look up the name 284952a6212SJordan K. Hubbard * and verify that it refers to a sensible block device. 28527a0bc89SDoug Rabson */ 28627a0bc89SDoug Rabson NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 28727a0bc89SDoug Rabson error = namei(ndp); 288952a6212SJordan K. Hubbard if (error) 289952a6212SJordan K. Hubbard return (error); 29027a0bc89SDoug Rabson devvp = ndp->ni_vp; 291952a6212SJordan K. Hubbard 29241d2e3e0SPoul-Henning Kamp if (!vn_isdisk(devvp)) { 29327a0bc89SDoug Rabson vrele(devvp); 294952a6212SJordan K. Hubbard return (ENOTBLK); 29527a0bc89SDoug Rabson } 29627a0bc89SDoug Rabson /* 297952a6212SJordan K. Hubbard * If mount by non-root, then verify that user has necessary 298952a6212SJordan K. Hubbard * permissions on the device. 29927a0bc89SDoug Rabson */ 300952a6212SJordan K. Hubbard if (p->p_ucred->cr_uid != 0) { 301952a6212SJordan K. Hubbard accessmode = VREAD; 302952a6212SJordan K. Hubbard if ((mp->mnt_flag & MNT_RDONLY) == 0) 303952a6212SJordan K. Hubbard accessmode |= VWRITE; 304952a6212SJordan K. Hubbard vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 305952a6212SJordan K. Hubbard error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p); 306952a6212SJordan K. Hubbard if (error) { 307952a6212SJordan K. Hubbard vput(devvp); 308952a6212SJordan K. Hubbard return (error); 309952a6212SJordan K. Hubbard } 310952a6212SJordan K. Hubbard VOP_UNLOCK(devvp, 0, p); 311952a6212SJordan K. Hubbard } 312952a6212SJordan K. Hubbard if ((mp->mnt_flag & MNT_UPDATE) == 0) { 313952a6212SJordan K. Hubbard error = mountmsdosfs(devvp, mp, p, &args); 314952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG /* only needed for the printf below */ 315952a6212SJordan K. Hubbard pmp = VFSTOMSDOSFS(mp); 316952a6212SJordan K. Hubbard #endif 317952a6212SJordan K. Hubbard } else { 31827a0bc89SDoug Rabson if (devvp != pmp->pm_devvp) 319952a6212SJordan K. Hubbard error = EINVAL; /* XXX needs translation */ 32027a0bc89SDoug Rabson else 32127a0bc89SDoug Rabson vrele(devvp); 32227a0bc89SDoug Rabson } 32327a0bc89SDoug Rabson if (error) { 32427a0bc89SDoug Rabson vrele(devvp); 325952a6212SJordan K. Hubbard return (error); 326952a6212SJordan K. Hubbard } 327952a6212SJordan K. Hubbard 328952a6212SJordan K. Hubbard error = update_mp(mp, &args); 329952a6212SJordan K. Hubbard if (error) { 330952a6212SJordan K. Hubbard msdosfs_unmount(mp, MNT_FORCE, p); 33127a0bc89SDoug Rabson return error; 33227a0bc89SDoug Rabson } 33327a0bc89SDoug Rabson 334952a6212SJordan K. Hubbard (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); 335952a6212SJordan K. Hubbard bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 336952a6212SJordan K. Hubbard (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 337952a6212SJordan K. Hubbard &size); 338952a6212SJordan K. Hubbard bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 33927a0bc89SDoug Rabson (void) msdosfs_statfs(mp, &mp->mnt_stat, p); 34027a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 341623ae52eSPoul-Henning Kamp printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap); 34227a0bc89SDoug Rabson #endif 343952a6212SJordan K. Hubbard return (0); 34427a0bc89SDoug Rabson } 34527a0bc89SDoug Rabson 3467fefffeeSPoul-Henning Kamp static int 347952a6212SJordan K. Hubbard mountmsdosfs(devvp, mp, p, argp) 34827a0bc89SDoug Rabson struct vnode *devvp; 34927a0bc89SDoug Rabson struct mount *mp; 35027a0bc89SDoug Rabson struct proc *p; 351952a6212SJordan K. Hubbard struct msdosfs_args *argp; 35227a0bc89SDoug Rabson { 353952a6212SJordan K. Hubbard struct msdosfsmount *pmp; 354952a6212SJordan K. Hubbard struct buf *bp; 35527a0bc89SDoug Rabson dev_t dev = devvp->v_rdev; 356952a6212SJordan K. Hubbard #ifndef __FreeBSD__ 357952a6212SJordan K. Hubbard struct partinfo dpart; 358f1d19042SArchie Cobbs int bsize = 0, dtype = 0, tmp; 359952a6212SJordan K. Hubbard #endif 36027a0bc89SDoug Rabson union bootsector *bsp; 36127a0bc89SDoug Rabson struct byte_bpb33 *b33; 36227a0bc89SDoug Rabson struct byte_bpb50 *b50; 363952a6212SJordan K. Hubbard struct byte_bpb710 *b710; 364952a6212SJordan K. Hubbard u_int8_t SecPerClust; 365952a6212SJordan K. Hubbard int ronly, error; 36627a0bc89SDoug Rabson 36727a0bc89SDoug Rabson /* 368952a6212SJordan K. Hubbard * Disallow multiple mounts of the same device. 369952a6212SJordan K. Hubbard * Disallow mounting of a device that is currently in use 370952a6212SJordan K. Hubbard * (except for root, which might share swap device for miniroot). 371952a6212SJordan K. Hubbard * Flush out any old buffers remaining from a previous use. 37227a0bc89SDoug Rabson */ 373c3c6d51eSPoul-Henning Kamp error = vfs_mountedon(devvp); 374c3c6d51eSPoul-Henning Kamp if (error) 375952a6212SJordan K. Hubbard return (error); 376952a6212SJordan K. Hubbard if (vcount(devvp) > 1 && devvp != rootvp) 377952a6212SJordan K. Hubbard return (EBUSY); 378952a6212SJordan K. Hubbard vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 379c3c6d51eSPoul-Henning Kamp error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0); 380952a6212SJordan K. Hubbard VOP_UNLOCK(devvp, 0, p); 381c3c6d51eSPoul-Henning Kamp if (error) 382952a6212SJordan K. Hubbard return (error); 38327a0bc89SDoug Rabson 384952a6212SJordan K. Hubbard ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 385698f9cf8SPoul-Henning Kamp vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p); 386c3c6d51eSPoul-Henning Kamp error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p); 387698f9cf8SPoul-Henning Kamp VOP_UNLOCK(devvp, 0, p); 388c3c6d51eSPoul-Henning Kamp if (error) 389952a6212SJordan K. Hubbard return (error); 390952a6212SJordan K. Hubbard 391952a6212SJordan K. Hubbard bp = NULL; /* both used in error_exit */ 392952a6212SJordan K. Hubbard pmp = NULL; 393952a6212SJordan K. Hubbard 394952a6212SJordan K. Hubbard #ifndef __FreeBSD__ 395952a6212SJordan K. Hubbard if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 39627a0bc89SDoug Rabson /* 397952a6212SJordan K. Hubbard * We need the disklabel to calculate the size of a FAT entry 398952a6212SJordan K. Hubbard * later on. Also make sure the partition contains a filesystem 399952a6212SJordan K. Hubbard * of type FS_MSDOS. This doesn't work for floppies, so we have 400952a6212SJordan K. Hubbard * to check for them too. 401952a6212SJordan K. Hubbard * 402952a6212SJordan K. Hubbard * At least some parts of the msdos fs driver seem to assume 403952a6212SJordan K. Hubbard * that the size of a disk block will always be 512 bytes. 404952a6212SJordan K. Hubbard * Let's check it... 40527a0bc89SDoug Rabson */ 406952a6212SJordan K. Hubbard error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, 407952a6212SJordan K. Hubbard FREAD, NOCRED, p); 408952a6212SJordan K. Hubbard if (error) 409952a6212SJordan K. Hubbard goto error_exit; 410952a6212SJordan K. Hubbard tmp = dpart.part->p_fstype; 411952a6212SJordan K. Hubbard dtype = dpart.disklab->d_type; 412952a6212SJordan K. Hubbard bsize = dpart.disklab->d_secsize; 413952a6212SJordan K. Hubbard if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) { 414952a6212SJordan K. Hubbard error = EINVAL; 415952a6212SJordan K. Hubbard goto error_exit; 416952a6212SJordan K. Hubbard } 41727a0bc89SDoug Rabson } 41827a0bc89SDoug Rabson #endif 41927a0bc89SDoug Rabson 42027a0bc89SDoug Rabson /* 421952a6212SJordan K. Hubbard * Read the boot sector of the filesystem, and then check the 422952a6212SJordan K. Hubbard * boot signature. If not a dos boot sector then error out. 42327a0bc89SDoug Rabson */ 424ad63a118SSatoshi Asami #ifdef PC98 425952a6212SJordan K. Hubbard error = bread(devvp, 0, 1024, NOCRED, &bp); 426ad63a118SSatoshi Asami #else 427952a6212SJordan K. Hubbard error = bread(devvp, 0, 512, NOCRED, &bp); 428ad63a118SSatoshi Asami #endif 429c3c6d51eSPoul-Henning Kamp if (error) 43027a0bc89SDoug Rabson goto error_exit; 431952a6212SJordan K. Hubbard bp->b_flags |= B_AGE; 432952a6212SJordan K. Hubbard bsp = (union bootsector *)bp->b_data; 43327a0bc89SDoug Rabson b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 43427a0bc89SDoug Rabson b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 435952a6212SJordan K. Hubbard b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP; 436952a6212SJordan K. Hubbard 437952a6212SJordan K. Hubbard #ifndef __FreeBSD__ 438952a6212SJordan K. Hubbard if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) { 439952a6212SJordan K. Hubbard #endif 440ad63a118SSatoshi Asami #ifdef PC98 441efa0ac5bSKATO Takenori if ((bsp->bs50.bsBootSectSig0 != BOOTSIG0 442efa0ac5bSKATO Takenori || bsp->bs50.bsBootSectSig1 != BOOTSIG1) 443efa0ac5bSKATO Takenori && (bsp->bs50.bsBootSectSig0 != 0 /* PC98 DOS 3.3x */ 444efa0ac5bSKATO Takenori || bsp->bs50.bsBootSectSig1 != 0) 445efa0ac5bSKATO Takenori && (bsp->bs50.bsBootSectSig0 != 0x90 /* PC98 DOS 5.0 */ 446efa0ac5bSKATO Takenori || bsp->bs50.bsBootSectSig1 != 0x3d) 447efa0ac5bSKATO Takenori && (bsp->bs50.bsBootSectSig0 != 0x46 /* PC98 DOS 3.3B */ 448efa0ac5bSKATO Takenori || bsp->bs50.bsBootSectSig1 != 0xfa)) { 449ad63a118SSatoshi Asami #else 450952a6212SJordan K. Hubbard if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 451952a6212SJordan K. Hubbard || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 452ad63a118SSatoshi Asami #endif 45327a0bc89SDoug Rabson error = EINVAL; 45427a0bc89SDoug Rabson goto error_exit; 45527a0bc89SDoug Rabson } 456952a6212SJordan K. Hubbard #ifndef __FreeBSD__ 45727a0bc89SDoug Rabson } 458952a6212SJordan K. Hubbard #endif 45927a0bc89SDoug Rabson 46027a0bc89SDoug Rabson pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK); 46127a0bc89SDoug Rabson bzero((caddr_t)pmp, sizeof *pmp); 46227a0bc89SDoug Rabson pmp->pm_mountp = mp; 46327a0bc89SDoug Rabson 46427a0bc89SDoug Rabson /* 46527a0bc89SDoug Rabson * Compute several useful quantities from the bpb in the 46627a0bc89SDoug Rabson * bootsector. Copy in the dos 5 variant of the bpb then fix up 46727a0bc89SDoug Rabson * the fields that are different between dos 5 and dos 3.3. 46827a0bc89SDoug Rabson */ 469952a6212SJordan K. Hubbard SecPerClust = b50->bpbSecPerClust; 47027a0bc89SDoug Rabson pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 47127a0bc89SDoug Rabson pmp->pm_ResSectors = getushort(b50->bpbResSectors); 47227a0bc89SDoug Rabson pmp->pm_FATs = b50->bpbFATs; 47327a0bc89SDoug Rabson pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 47427a0bc89SDoug Rabson pmp->pm_Sectors = getushort(b50->bpbSectors); 47527a0bc89SDoug Rabson pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 47627a0bc89SDoug Rabson pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 47727a0bc89SDoug Rabson pmp->pm_Heads = getushort(b50->bpbHeads); 478952a6212SJordan K. Hubbard pmp->pm_Media = b50->bpbMedia; 47927a0bc89SDoug Rabson 480952a6212SJordan K. Hubbard #ifndef __FreeBSD__ 481952a6212SJordan K. Hubbard if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) { 482952a6212SJordan K. Hubbard #endif 48327a0bc89SDoug Rabson /* XXX - We should probably check more values here */ 484952a6212SJordan K. Hubbard if (!pmp->pm_BytesPerSec || !SecPerClust 485952a6212SJordan K. Hubbard || !pmp->pm_Heads || pmp->pm_Heads > 255 486ad63a118SSatoshi Asami #ifdef PC98 487952a6212SJordan K. Hubbard || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) { 488ad63a118SSatoshi Asami #else 489952a6212SJordan K. Hubbard || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) { 490ad63a118SSatoshi Asami #endif 49127a0bc89SDoug Rabson error = EINVAL; 49227a0bc89SDoug Rabson goto error_exit; 49327a0bc89SDoug Rabson } 494952a6212SJordan K. Hubbard #ifndef __FreeBSD__ 495952a6212SJordan K. Hubbard } 496952a6212SJordan K. Hubbard #endif 49727a0bc89SDoug Rabson 49827a0bc89SDoug Rabson if (pmp->pm_Sectors == 0) { 49927a0bc89SDoug Rabson pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); 50027a0bc89SDoug Rabson pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); 50127a0bc89SDoug Rabson } else { 50227a0bc89SDoug Rabson pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); 50327a0bc89SDoug Rabson pmp->pm_HugeSectors = pmp->pm_Sectors; 50427a0bc89SDoug Rabson } 505c681be37SDmitrij Tejblum if (pmp->pm_HugeSectors > 0xffffffff / 506c681be37SDmitrij Tejblum (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) { 507952a6212SJordan K. Hubbard /* 508952a6212SJordan K. Hubbard * We cannot deal currently with this size of disk 509952a6212SJordan K. Hubbard * due to fileid limitations (see msdosfs_getattr and 510952a6212SJordan K. Hubbard * msdosfs_readdir) 511952a6212SJordan K. Hubbard */ 512952a6212SJordan K. Hubbard error = EINVAL; 513aaf0bb19SAndrey A. Chernov printf("mountmsdosfs(): disk too big, sorry\n"); 514952a6212SJordan K. Hubbard goto error_exit; 515952a6212SJordan K. Hubbard } 516952a6212SJordan K. Hubbard 517952a6212SJordan K. Hubbard if (pmp->pm_RootDirEnts == 0) { 518952a6212SJordan K. Hubbard if (bsp->bs710.bsBootSectSig2 != BOOTSIG2 519952a6212SJordan K. Hubbard || bsp->bs710.bsBootSectSig3 != BOOTSIG3 520952a6212SJordan K. Hubbard || pmp->pm_Sectors 521952a6212SJordan K. Hubbard || pmp->pm_FATsecs 522952a6212SJordan K. Hubbard || getushort(b710->bpbFSVers)) { 523952a6212SJordan K. Hubbard error = EINVAL; 524aaf0bb19SAndrey A. Chernov printf("mountmsdosfs(): bad FAT32 filesystem\n"); 525952a6212SJordan K. Hubbard goto error_exit; 526952a6212SJordan K. Hubbard } 527952a6212SJordan K. Hubbard pmp->pm_fatmask = FAT32_MASK; 528952a6212SJordan K. Hubbard pmp->pm_fatmult = 4; 529952a6212SJordan K. Hubbard pmp->pm_fatdiv = 1; 530952a6212SJordan K. Hubbard pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 531952a6212SJordan K. Hubbard if (getushort(b710->bpbExtFlags) & FATMIRROR) 532952a6212SJordan K. Hubbard pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 533952a6212SJordan K. Hubbard else 534952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFS_FATMIRROR; 535952a6212SJordan K. Hubbard } else 536952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFS_FATMIRROR; 537952a6212SJordan K. Hubbard 538952a6212SJordan K. Hubbard #ifndef __FreeBSD__ 539952a6212SJordan K. Hubbard if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 540952a6212SJordan K. Hubbard if (FAT32(pmp)) { 541952a6212SJordan K. Hubbard /* 542952a6212SJordan K. Hubbard * GEMDOS doesn't know fat32. 543952a6212SJordan K. Hubbard */ 544952a6212SJordan K. Hubbard error = EINVAL; 545952a6212SJordan K. Hubbard goto error_exit; 546952a6212SJordan K. Hubbard } 547952a6212SJordan K. Hubbard 548952a6212SJordan K. Hubbard /* 549952a6212SJordan K. Hubbard * Check a few values (could do some more): 550952a6212SJordan K. Hubbard * - logical sector size: power of 2, >= block size 551952a6212SJordan K. Hubbard * - sectors per cluster: power of 2, >= 1 552952a6212SJordan K. Hubbard * - number of sectors: >= 1, <= size of partition 553952a6212SJordan K. Hubbard */ 554952a6212SJordan K. Hubbard if ( (SecPerClust == 0) 555952a6212SJordan K. Hubbard || (SecPerClust & (SecPerClust - 1)) 556952a6212SJordan K. Hubbard || (pmp->pm_BytesPerSec < bsize) 557952a6212SJordan K. Hubbard || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 558952a6212SJordan K. Hubbard || (pmp->pm_HugeSectors == 0) 559952a6212SJordan K. Hubbard || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize) 560952a6212SJordan K. Hubbard > dpart.part->p_size) 561952a6212SJordan K. Hubbard ) { 562952a6212SJordan K. Hubbard error = EINVAL; 563952a6212SJordan K. Hubbard goto error_exit; 564952a6212SJordan K. Hubbard } 565952a6212SJordan K. Hubbard /* 566952a6212SJordan K. Hubbard * XXX - Many parts of the msdos fs driver seem to assume that 567952a6212SJordan K. Hubbard * the number of bytes per logical sector (BytesPerSec) will 568952a6212SJordan K. Hubbard * always be the same as the number of bytes per disk block 569952a6212SJordan K. Hubbard * Let's pretend it is. 570952a6212SJordan K. Hubbard */ 571952a6212SJordan K. Hubbard tmp = pmp->pm_BytesPerSec / bsize; 572952a6212SJordan K. Hubbard pmp->pm_BytesPerSec = bsize; 573952a6212SJordan K. Hubbard pmp->pm_HugeSectors *= tmp; 574952a6212SJordan K. Hubbard pmp->pm_HiddenSects *= tmp; 575952a6212SJordan K. Hubbard pmp->pm_ResSectors *= tmp; 576952a6212SJordan K. Hubbard pmp->pm_Sectors *= tmp; 577952a6212SJordan K. Hubbard pmp->pm_FATsecs *= tmp; 578952a6212SJordan K. Hubbard SecPerClust *= tmp; 579952a6212SJordan K. Hubbard } 580952a6212SJordan K. Hubbard #endif 58127a0bc89SDoug Rabson pmp->pm_fatblk = pmp->pm_ResSectors; 582952a6212SJordan K. Hubbard if (FAT32(pmp)) { 583952a6212SJordan K. Hubbard pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 584952a6212SJordan K. Hubbard pmp->pm_firstcluster = pmp->pm_fatblk 585952a6212SJordan K. Hubbard + (pmp->pm_FATs * pmp->pm_FATsecs); 586952a6212SJordan K. Hubbard pmp->pm_fsinfo = getushort(b710->bpbFSInfo); 587952a6212SJordan K. Hubbard } else { 58827a0bc89SDoug Rabson pmp->pm_rootdirblk = pmp->pm_fatblk + 58927a0bc89SDoug Rabson (pmp->pm_FATs * pmp->pm_FATsecs); 590952a6212SJordan K. Hubbard pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 591952a6212SJordan K. Hubbard + pmp->pm_BytesPerSec - 1) 592952a6212SJordan K. Hubbard / pmp->pm_BytesPerSec;/* in sectors */ 59327a0bc89SDoug Rabson pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 594952a6212SJordan K. Hubbard } 595952a6212SJordan K. Hubbard 59627a0bc89SDoug Rabson pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 597952a6212SJordan K. Hubbard SecPerClust; 59827a0bc89SDoug Rabson pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1; 59927a0bc89SDoug Rabson pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec; 600952a6212SJordan K. Hubbard 601952a6212SJordan K. Hubbard #ifndef __FreeBSD__ 602952a6212SJordan K. Hubbard if (argp->flags & MSDOSFSMNT_GEMDOSFS) { 603952a6212SJordan K. Hubbard if ((pmp->pm_nmbrofclusters <= (0xff0 - 2)) 604952a6212SJordan K. Hubbard && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE) 605952a6212SJordan K. Hubbard && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2)))) 606952a6212SJordan K. Hubbard ) { 607952a6212SJordan K. Hubbard pmp->pm_fatmask = FAT12_MASK; 608952a6212SJordan K. Hubbard pmp->pm_fatmult = 3; 609952a6212SJordan K. Hubbard pmp->pm_fatdiv = 2; 610952a6212SJordan K. Hubbard } else { 611952a6212SJordan K. Hubbard pmp->pm_fatmask = FAT16_MASK; 612952a6212SJordan K. Hubbard pmp->pm_fatmult = 2; 613952a6212SJordan K. Hubbard pmp->pm_fatdiv = 1; 614952a6212SJordan K. Hubbard } 615952a6212SJordan K. Hubbard } else 616952a6212SJordan K. Hubbard #endif 617952a6212SJordan K. Hubbard if (pmp->pm_fatmask == 0) { 618952a6212SJordan K. Hubbard if (pmp->pm_maxcluster 619952a6212SJordan K. Hubbard <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 62027a0bc89SDoug Rabson /* 621952a6212SJordan K. Hubbard * This will usually be a floppy disk. This size makes 622952a6212SJordan K. Hubbard * sure that one fat entry will not be split across 623952a6212SJordan K. Hubbard * multiple blocks. 62427a0bc89SDoug Rabson */ 625952a6212SJordan K. Hubbard pmp->pm_fatmask = FAT12_MASK; 626952a6212SJordan K. Hubbard pmp->pm_fatmult = 3; 627952a6212SJordan K. Hubbard pmp->pm_fatdiv = 2; 628952a6212SJordan K. Hubbard } else { 629952a6212SJordan K. Hubbard pmp->pm_fatmask = FAT16_MASK; 630952a6212SJordan K. Hubbard pmp->pm_fatmult = 2; 631952a6212SJordan K. Hubbard pmp->pm_fatdiv = 1; 632952a6212SJordan K. Hubbard } 633952a6212SJordan K. Hubbard } 634952a6212SJordan K. Hubbard if (FAT12(pmp)) 63527a0bc89SDoug Rabson pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec; 63627a0bc89SDoug Rabson else 637c681be37SDmitrij Tejblum pmp->pm_fatblocksize = DFLTBSIZE; 638952a6212SJordan K. Hubbard 63927a0bc89SDoug Rabson pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec; 640952a6212SJordan K. Hubbard pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1; 64127a0bc89SDoug Rabson 64227a0bc89SDoug Rabson /* 64327a0bc89SDoug Rabson * Compute mask and shift value for isolating cluster relative byte 64427a0bc89SDoug Rabson * offsets and cluster numbers from a file offset. 64527a0bc89SDoug Rabson */ 646952a6212SJordan K. Hubbard pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec; 647952a6212SJordan K. Hubbard pmp->pm_crbomask = pmp->pm_bpcluster - 1; 648952a6212SJordan K. Hubbard pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 64927a0bc89SDoug Rabson 650952a6212SJordan K. Hubbard /* 651952a6212SJordan K. Hubbard * Check for valid cluster size 652952a6212SJordan K. Hubbard * must be a power of 2 653952a6212SJordan K. Hubbard */ 654952a6212SJordan K. Hubbard if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 655952a6212SJordan K. Hubbard error = EINVAL; 656952a6212SJordan K. Hubbard goto error_exit; 657ad63a118SSatoshi Asami } 65827a0bc89SDoug Rabson 65927a0bc89SDoug Rabson /* 66027a0bc89SDoug Rabson * Release the bootsector buffer. 66127a0bc89SDoug Rabson */ 662952a6212SJordan K. Hubbard brelse(bp); 663952a6212SJordan K. Hubbard bp = NULL; 664952a6212SJordan K. Hubbard 665952a6212SJordan K. Hubbard /* 666952a6212SJordan K. Hubbard * Check FSInfo. 667952a6212SJordan K. Hubbard */ 668952a6212SJordan K. Hubbard if (pmp->pm_fsinfo) { 669952a6212SJordan K. Hubbard struct fsinfo *fp; 670952a6212SJordan K. Hubbard 671952a6212SJordan K. Hubbard if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0) 672952a6212SJordan K. Hubbard goto error_exit; 673952a6212SJordan K. Hubbard fp = (struct fsinfo *)bp->b_data; 674952a6212SJordan K. Hubbard if (!bcmp(fp->fsisig1, "RRaA", 4) 675952a6212SJordan K. Hubbard && !bcmp(fp->fsisig2, "rrAa", 4) 676952a6212SJordan K. Hubbard && !bcmp(fp->fsisig3, "\0\0\125\252", 4) 677952a6212SJordan K. Hubbard && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) 678952a6212SJordan K. Hubbard pmp->pm_nxtfree = getulong(fp->fsinxtfree); 679952a6212SJordan K. Hubbard else 680952a6212SJordan K. Hubbard pmp->pm_fsinfo = 0; 681952a6212SJordan K. Hubbard brelse(bp); 682952a6212SJordan K. Hubbard bp = NULL; 683952a6212SJordan K. Hubbard } 684952a6212SJordan K. Hubbard 685952a6212SJordan K. Hubbard /* 686952a6212SJordan K. Hubbard * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX 687952a6212SJordan K. Hubbard */ 68827a0bc89SDoug Rabson 68927a0bc89SDoug Rabson /* 69027a0bc89SDoug Rabson * Allocate memory for the bitmap of allocated clusters, and then 69127a0bc89SDoug Rabson * fill it in. 69227a0bc89SDoug Rabson */ 69327a0bc89SDoug Rabson pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1) 69427a0bc89SDoug Rabson / N_INUSEBITS) 69527a0bc89SDoug Rabson * sizeof(*pmp->pm_inusemap), 69627a0bc89SDoug Rabson M_MSDOSFSFAT, M_WAITOK); 69727a0bc89SDoug Rabson 69827a0bc89SDoug Rabson /* 69927a0bc89SDoug Rabson * fillinusemap() needs pm_devvp. 70027a0bc89SDoug Rabson */ 70127a0bc89SDoug Rabson pmp->pm_dev = dev; 70227a0bc89SDoug Rabson pmp->pm_devvp = devvp; 70327a0bc89SDoug Rabson 70427a0bc89SDoug Rabson /* 70527a0bc89SDoug Rabson * Have the inuse map filled in. 70627a0bc89SDoug Rabson */ 707952a6212SJordan K. Hubbard if ((error = fillinusemap(pmp)) != 0) 70827a0bc89SDoug Rabson goto error_exit; 70927a0bc89SDoug Rabson 71027a0bc89SDoug Rabson /* 71127a0bc89SDoug Rabson * If they want fat updates to be synchronous then let them suffer 71227a0bc89SDoug Rabson * the performance degradation in exchange for the on disk copy of 71327a0bc89SDoug Rabson * the fat being correct just about all the time. I suppose this 71427a0bc89SDoug Rabson * would be a good thing to turn on if the kernel is still flakey. 71527a0bc89SDoug Rabson */ 716952a6212SJordan K. Hubbard if (mp->mnt_flag & MNT_SYNCHRONOUS) 717952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 71827a0bc89SDoug Rabson 71927a0bc89SDoug Rabson /* 72027a0bc89SDoug Rabson * Finish up. 72127a0bc89SDoug Rabson */ 722952a6212SJordan K. Hubbard if (ronly) 723952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFSMNT_RONLY; 724952a6212SJordan K. Hubbard else 72527a0bc89SDoug Rabson pmp->pm_fmod = 1; 72627a0bc89SDoug Rabson mp->mnt_data = (qaddr_t) pmp; 727939cb752SBruce Evans mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 728af3f60d5SBruce Evans mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 729cc9d8990SPeter Wemm mp->mnt_flag |= MNT_LOCAL; 730b1897c19SJulian Elischer devvp->v_specmountpoint = mp; 73127a0bc89SDoug Rabson 73227a0bc89SDoug Rabson return 0; 73327a0bc89SDoug Rabson 734952a6212SJordan K. Hubbard error_exit: 735952a6212SJordan K. Hubbard if (bp) 736952a6212SJordan K. Hubbard brelse(bp); 737952a6212SJordan K. Hubbard (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p); 73827a0bc89SDoug Rabson if (pmp) { 73927a0bc89SDoug Rabson if (pmp->pm_inusemap) 740952a6212SJordan K. Hubbard free(pmp->pm_inusemap, M_MSDOSFSFAT); 741952a6212SJordan K. Hubbard free(pmp, M_MSDOSFSMNT); 74227a0bc89SDoug Rabson mp->mnt_data = (qaddr_t)0; 74327a0bc89SDoug Rabson } 744952a6212SJordan K. Hubbard return (error); 74527a0bc89SDoug Rabson } 74627a0bc89SDoug Rabson 74727a0bc89SDoug Rabson /* 74827a0bc89SDoug Rabson * Unmount the filesystem described by mp. 74927a0bc89SDoug Rabson */ 7507fefffeeSPoul-Henning Kamp static int 75127a0bc89SDoug Rabson msdosfs_unmount(mp, mntflags, p) 75227a0bc89SDoug Rabson struct mount *mp; 75327a0bc89SDoug Rabson int mntflags; 75427a0bc89SDoug Rabson struct proc *p; 75527a0bc89SDoug Rabson { 756952a6212SJordan K. Hubbard struct msdosfsmount *pmp; 757952a6212SJordan K. Hubbard int error, flags; 75827a0bc89SDoug Rabson 759952a6212SJordan K. Hubbard flags = 0; 760952a6212SJordan K. Hubbard if (mntflags & MNT_FORCE) 76127a0bc89SDoug Rabson flags |= FORCECLOSE; 762c3c6d51eSPoul-Henning Kamp error = vflush(mp, NULLVP, flags); 763c3c6d51eSPoul-Henning Kamp if (error) 76427a0bc89SDoug Rabson return error; 765952a6212SJordan K. Hubbard pmp = VFSTOMSDOSFS(mp); 766b1897c19SJulian Elischer pmp->pm_devvp->v_specmountpoint = NULL; 767952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG 768952a6212SJordan K. Hubbard { 769952a6212SJordan K. Hubbard struct vnode *vp = pmp->pm_devvp; 770952a6212SJordan K. Hubbard 771952a6212SJordan K. Hubbard printf("msdosfs_umount(): just before calling VOP_CLOSE()\n"); 772952a6212SJordan K. Hubbard printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n", 773952a6212SJordan K. Hubbard vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt); 77467ddfcafSMatthew Dillon printf("id %lu, mount %p, op %p\n", 77567ddfcafSMatthew Dillon vp->v_id, vp->v_mount, vp->v_op); 776952a6212SJordan K. Hubbard printf("freef %p, freeb %p, mount %p\n", 777952a6212SJordan K. Hubbard vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev, 778952a6212SJordan K. Hubbard vp->v_mount); 779952a6212SJordan K. Hubbard printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n", 78040c8cfe5SPeter Wemm TAILQ_FIRST(&vp->v_cleanblkhd), 78140c8cfe5SPeter Wemm TAILQ_FIRST(&vp->v_dirtyblkhd), 782952a6212SJordan K. Hubbard vp->v_numoutput, vp->v_type); 783952a6212SJordan K. Hubbard printf("union %p, tag %d, data[0] %08x, data[1] %08x\n", 784952a6212SJordan K. Hubbard vp->v_socket, vp->v_tag, 785952a6212SJordan K. Hubbard ((u_int *)vp->v_data)[0], 786952a6212SJordan K. Hubbard ((u_int *)vp->v_data)[1]); 787952a6212SJordan K. Hubbard } 788952a6212SJordan K. Hubbard #endif 789b1897c19SJulian Elischer error = VOP_CLOSE(pmp->pm_devvp, 790b1897c19SJulian Elischer (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE, 79127a0bc89SDoug Rabson NOCRED, p); 79227a0bc89SDoug Rabson vrele(pmp->pm_devvp); 793952a6212SJordan K. Hubbard free(pmp->pm_inusemap, M_MSDOSFSFAT); 794952a6212SJordan K. Hubbard free(pmp, M_MSDOSFSMNT); 79527a0bc89SDoug Rabson mp->mnt_data = (qaddr_t)0; 796cc9d8990SPeter Wemm mp->mnt_flag &= ~MNT_LOCAL; 797952a6212SJordan K. Hubbard return (error); 79827a0bc89SDoug Rabson } 79927a0bc89SDoug Rabson 8007fefffeeSPoul-Henning Kamp static int 80127a0bc89SDoug Rabson msdosfs_root(mp, vpp) 80227a0bc89SDoug Rabson struct mount *mp; 80327a0bc89SDoug Rabson struct vnode **vpp; 80427a0bc89SDoug Rabson { 805952a6212SJordan K. Hubbard struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 80627a0bc89SDoug Rabson struct denode *ndep; 80727a0bc89SDoug Rabson int error; 80827a0bc89SDoug Rabson 80927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 810952a6212SJordan K. Hubbard printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 81127a0bc89SDoug Rabson #endif 812952a6212SJordan K. Hubbard error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 813952a6212SJordan K. Hubbard if (error) 814952a6212SJordan K. Hubbard return (error); 81527a0bc89SDoug Rabson *vpp = DETOV(ndep); 816952a6212SJordan K. Hubbard return (0); 81727a0bc89SDoug Rabson } 81827a0bc89SDoug Rabson 8197fefffeeSPoul-Henning Kamp static int 82027a0bc89SDoug Rabson msdosfs_statfs(mp, sbp, p) 82127a0bc89SDoug Rabson struct mount *mp; 82227a0bc89SDoug Rabson struct statfs *sbp; 82327a0bc89SDoug Rabson struct proc *p; 82427a0bc89SDoug Rabson { 825952a6212SJordan K. Hubbard struct msdosfsmount *pmp; 82627a0bc89SDoug Rabson 827952a6212SJordan K. Hubbard pmp = VFSTOMSDOSFS(mp); 82827a0bc89SDoug Rabson sbp->f_bsize = pmp->pm_bpcluster; 82927a0bc89SDoug Rabson sbp->f_iosize = pmp->pm_bpcluster; 83027a0bc89SDoug Rabson sbp->f_blocks = pmp->pm_nmbrofclusters; 83127a0bc89SDoug Rabson sbp->f_bfree = pmp->pm_freeclustercount; 83227a0bc89SDoug Rabson sbp->f_bavail = pmp->pm_freeclustercount; 83327a0bc89SDoug Rabson sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 83427a0bc89SDoug Rabson sbp->f_ffree = 0; /* what to put in here? */ 83527a0bc89SDoug Rabson if (sbp != &mp->mnt_stat) { 836af3f60d5SBruce Evans sbp->f_type = mp->mnt_vfc->vfc_typenum; 837952a6212SJordan K. Hubbard bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 838952a6212SJordan K. Hubbard bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 83927a0bc89SDoug Rabson } 840952a6212SJordan K. Hubbard strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 841952a6212SJordan K. Hubbard return (0); 84227a0bc89SDoug Rabson } 84327a0bc89SDoug Rabson 8447fefffeeSPoul-Henning Kamp static int 84527a0bc89SDoug Rabson msdosfs_sync(mp, waitfor, cred, p) 84627a0bc89SDoug Rabson struct mount *mp; 84727a0bc89SDoug Rabson int waitfor; 84827a0bc89SDoug Rabson struct ucred *cred; 84927a0bc89SDoug Rabson struct proc *p; 85027a0bc89SDoug Rabson { 851952a6212SJordan K. Hubbard struct vnode *vp, *nvp; 85227a0bc89SDoug Rabson struct denode *dep; 853952a6212SJordan K. Hubbard struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 854952a6212SJordan K. Hubbard int error, allerror = 0; 85527a0bc89SDoug Rabson 85627a0bc89SDoug Rabson /* 85727a0bc89SDoug Rabson * If we ever switch to not updating all of the fats all the time, 85827a0bc89SDoug Rabson * this would be the place to update them from the first one. 85927a0bc89SDoug Rabson */ 860dfd5dee1SPeter Wemm if (pmp->pm_fmod != 0) { 861952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_RONLY) 86227a0bc89SDoug Rabson panic("msdosfs_sync: rofs mod"); 86327a0bc89SDoug Rabson else { 86427a0bc89SDoug Rabson /* update fats here */ 86527a0bc89SDoug Rabson } 866dfd5dee1SPeter Wemm } 86727a0bc89SDoug Rabson /* 868952a6212SJordan K. Hubbard * Write back each (modified) denode. 86927a0bc89SDoug Rabson */ 870af3f60d5SBruce Evans simple_lock(&mntvnode_slock); 87127a0bc89SDoug Rabson loop: 872c681be37SDmitrij Tejblum for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) { 873952a6212SJordan K. Hubbard /* 874952a6212SJordan K. Hubbard * If the vnode that we are about to sync is no longer 875c681be37SDmitrij Tejblum * associated with this mount point, start over. 876952a6212SJordan K. Hubbard */ 877952a6212SJordan K. Hubbard if (vp->v_mount != mp) 87827a0bc89SDoug Rabson goto loop; 879952a6212SJordan K. Hubbard 880af3f60d5SBruce Evans simple_lock(&vp->v_interlock); 881952a6212SJordan K. Hubbard nvp = vp->v_mntvnodes.le_next; 88227a0bc89SDoug Rabson dep = VTODE(vp); 883c681be37SDmitrij Tejblum if (vp->v_type == VNON || 884831a80b0SMatthew Dillon ((dep->de_flag & 885c681be37SDmitrij Tejblum (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 886831a80b0SMatthew Dillon (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) { 887af3f60d5SBruce Evans simple_unlock(&vp->v_interlock); 88827a0bc89SDoug Rabson continue; 889af3f60d5SBruce Evans } 890af3f60d5SBruce Evans simple_unlock(&mntvnode_slock); 891af3f60d5SBruce Evans error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p); 892af3f60d5SBruce Evans if (error) { 893af3f60d5SBruce Evans simple_lock(&mntvnode_slock); 894af3f60d5SBruce Evans if (error == ENOENT) 89527a0bc89SDoug Rabson goto loop; 896af3f60d5SBruce Evans continue; 897af3f60d5SBruce Evans } 898c3c6d51eSPoul-Henning Kamp error = VOP_FSYNC(vp, cred, waitfor, p); 899c3c6d51eSPoul-Henning Kamp if (error) 90027a0bc89SDoug Rabson allerror = error; 901af3f60d5SBruce Evans VOP_UNLOCK(vp, 0, p); 902c681be37SDmitrij Tejblum vrele(vp); 903af3f60d5SBruce Evans simple_lock(&mntvnode_slock); 90427a0bc89SDoug Rabson } 905af3f60d5SBruce Evans simple_unlock(&mntvnode_slock); 90627a0bc89SDoug Rabson 90727a0bc89SDoug Rabson /* 90827a0bc89SDoug Rabson * Flush filesystem control info. 90927a0bc89SDoug Rabson */ 910c681be37SDmitrij Tejblum if (waitfor != MNT_LAZY) { 911c681be37SDmitrij Tejblum vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p); 912c3c6d51eSPoul-Henning Kamp error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p); 913c3c6d51eSPoul-Henning Kamp if (error) 91427a0bc89SDoug Rabson allerror = error; 915c681be37SDmitrij Tejblum VOP_UNLOCK(pmp->pm_devvp, 0, p); 916c681be37SDmitrij Tejblum } 917952a6212SJordan K. Hubbard return (allerror); 91827a0bc89SDoug Rabson } 91927a0bc89SDoug Rabson 9207fefffeeSPoul-Henning Kamp static int 921c24fda81SAlfred Perlstein msdosfs_fhtovp(mp, fhp, vpp) 92227a0bc89SDoug Rabson struct mount *mp; 92327a0bc89SDoug Rabson struct fid *fhp; 92427a0bc89SDoug Rabson struct vnode **vpp; 92527a0bc89SDoug Rabson { 926952a6212SJordan K. Hubbard struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 92727a0bc89SDoug Rabson struct defid *defhp = (struct defid *) fhp; 92827a0bc89SDoug Rabson struct denode *dep; 92927a0bc89SDoug Rabson int error; 93027a0bc89SDoug Rabson 931952a6212SJordan K. Hubbard error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 93227a0bc89SDoug Rabson if (error) { 93327a0bc89SDoug Rabson *vpp = NULLVP; 934952a6212SJordan K. Hubbard return (error); 93527a0bc89SDoug Rabson } 93627a0bc89SDoug Rabson *vpp = DETOV(dep); 937c24fda81SAlfred Perlstein return (0); 938c24fda81SAlfred Perlstein } 939c24fda81SAlfred Perlstein 940c24fda81SAlfred Perlstein static int 941c24fda81SAlfred Perlstein msdosfs_checkexp(mp, nam, exflagsp, credanonp) 942c24fda81SAlfred Perlstein struct mount *mp; 943c24fda81SAlfred Perlstein struct sockaddr *nam; 944c24fda81SAlfred Perlstein int *exflagsp; 945c24fda81SAlfred Perlstein struct ucred **credanonp; 946c24fda81SAlfred Perlstein { 947c24fda81SAlfred Perlstein struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 948c24fda81SAlfred Perlstein struct netcred *np; 949c24fda81SAlfred Perlstein 950c24fda81SAlfred Perlstein np = vfs_export_lookup(mp, &pmp->pm_export, nam); 951c24fda81SAlfred Perlstein if (np == NULL) 952c24fda81SAlfred Perlstein return (EACCES); 95327a0bc89SDoug Rabson *exflagsp = np->netc_exflags; 95427a0bc89SDoug Rabson *credanonp = &np->netc_anon; 955952a6212SJordan K. Hubbard return (0); 95627a0bc89SDoug Rabson } 95727a0bc89SDoug Rabson 9587fefffeeSPoul-Henning Kamp static int 95927a0bc89SDoug Rabson msdosfs_vptofh(vp, fhp) 96027a0bc89SDoug Rabson struct vnode *vp; 96127a0bc89SDoug Rabson struct fid *fhp; 96227a0bc89SDoug Rabson { 963952a6212SJordan K. Hubbard struct denode *dep; 964952a6212SJordan K. Hubbard struct defid *defhp; 96527a0bc89SDoug Rabson 966952a6212SJordan K. Hubbard dep = VTODE(vp); 967952a6212SJordan K. Hubbard defhp = (struct defid *)fhp; 96827a0bc89SDoug Rabson defhp->defid_len = sizeof(struct defid); 96927a0bc89SDoug Rabson defhp->defid_dirclust = dep->de_dirclust; 97027a0bc89SDoug Rabson defhp->defid_dirofs = dep->de_diroffset; 971952a6212SJordan K. Hubbard /* defhp->defid_gen = dep->de_gen; */ 972952a6212SJordan K. Hubbard return (0); 97327a0bc89SDoug Rabson } 97427a0bc89SDoug Rabson 97530ffadf3SPoul-Henning Kamp static struct vfsops msdosfs_vfsops = { 97627a0bc89SDoug Rabson msdosfs_mount, 9775a5fccc8SAlfred Perlstein vfs_stdstart, 97827a0bc89SDoug Rabson msdosfs_unmount, 97927a0bc89SDoug Rabson msdosfs_root, 9805a5fccc8SAlfred Perlstein vfs_stdquotactl, 98127a0bc89SDoug Rabson msdosfs_statfs, 98227a0bc89SDoug Rabson msdosfs_sync, 9835a5fccc8SAlfred Perlstein vfs_stdvget, 98427a0bc89SDoug Rabson msdosfs_fhtovp, 985c24fda81SAlfred Perlstein msdosfs_checkexp, 98627a0bc89SDoug Rabson msdosfs_vptofh, 98727a0bc89SDoug Rabson msdosfs_init 98827a0bc89SDoug Rabson }; 989c901836cSGarrett Wollman 9908994ca3cSBruce Evans VFS_SET(msdosfs_vfsops, msdos, 0); 991