Home | History | Annotate | Line # | Download | only in msdosfs
msdosfs_vfsops.c revision 1.5
      1 /*	$NetBSD: msdosfs_vfsops.c,v 1.5 2003/06/28 14:21:50 darrenr Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
      5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
      6  * All rights reserved.
      7  * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by TooLs GmbH.
     20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 /*
     35  * Written by Paul Popelka (paulp (at) uts.amdahl.com)
     36  *
     37  * You can do anything you want with this software, just don't say you wrote
     38  * it, and don't remove this notice.
     39  *
     40  * This software is provided "as is".
     41  *
     42  * The author supplies this software to be publicly redistributed on the
     43  * understanding that the author is not responsible for the correct
     44  * functioning of this software in any circumstances and is not liable for
     45  * any damages caused by this software.
     46  *
     47  * October 1992
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_vfsops.c,v 1.5 2003/06/28 14:21:50 darrenr Exp $");
     52 
     53 #if defined(_KERNEL_OPT)
     54 #include "opt_quota.h"
     55 #include "opt_compat_netbsd.h"
     56 #endif
     57 
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/namei.h>
     61 #include <sys/proc.h>
     62 #include <sys/kernel.h>
     63 #include <sys/vnode.h>
     64 #include <miscfs/specfs/specdev.h> /* XXX */	/* defines v_rdev */
     65 #include <sys/mount.h>
     66 #include <sys/buf.h>
     67 #include <sys/file.h>
     68 #include <sys/device.h>
     69 #include <sys/disklabel.h>
     70 #include <sys/ioctl.h>
     71 #include <sys/malloc.h>
     72 #include <sys/dirent.h>
     73 #include <sys/stat.h>
     74 #include <sys/conf.h>
     75 
     76 #include <fs/msdosfs/bpb.h>
     77 #include <fs/msdosfs/bootsect.h>
     78 #include <fs/msdosfs/direntry.h>
     79 #include <fs/msdosfs/denode.h>
     80 #include <fs/msdosfs/msdosfsmount.h>
     81 #include <fs/msdosfs/fat.h>
     82 
     83 int msdosfs_mountroot __P((void));
     84 int msdosfs_mount __P((struct mount *, const char *, void *,
     85     struct nameidata *, struct lwp *));
     86 int msdosfs_start __P((struct mount *, int, struct lwp *));
     87 int msdosfs_unmount __P((struct mount *, int, struct lwp *));
     88 int msdosfs_root __P((struct mount *, struct vnode **, struct lwp *));
     89 int msdosfs_quotactl __P((struct mount *, int, uid_t, caddr_t, struct lwp *));
     90 int msdosfs_statfs __P((struct mount *, struct statfs *, struct lwp *));
     91 int msdosfs_sync __P((struct mount *, int, struct ucred *, struct lwp *));
     92 int msdosfs_vget __P((struct mount *, ino_t, struct vnode **, struct lwp *));
     93 int msdosfs_fhtovp __P((struct mount *, struct fid *, struct vnode **, struct lwp *));
     94 int msdosfs_checkexp __P((struct mount *, struct mbuf *, int *,
     95     struct ucred **));
     96 int msdosfs_vptofh __P((struct vnode *, struct fid *));
     97 int msdosfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
     98 			struct lwp *));
     99 
    100 int msdosfs_mountfs __P((struct vnode *, struct mount *, struct lwp *,
    101     struct msdosfs_args *));
    102 
    103 static int update_mp __P((struct mount *, struct msdosfs_args *, struct lwp *));
    104 
    105 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOS FS mount structure");
    106 MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS fat", "MSDOS FS fat table");
    107 
    108 #define ROOTNAME "root_device"
    109 
    110 extern const struct vnodeopv_desc msdosfs_vnodeop_opv_desc;
    111 
    112 const struct vnodeopv_desc * const msdosfs_vnodeopv_descs[] = {
    113 	&msdosfs_vnodeop_opv_desc,
    114 	NULL,
    115 };
    116 
    117 struct vfsops msdosfs_vfsops = {
    118 	MOUNT_MSDOS,
    119 	msdosfs_mount,
    120 	msdosfs_start,
    121 	msdosfs_unmount,
    122 	msdosfs_root,
    123 	msdosfs_quotactl,
    124 	msdosfs_statfs,
    125 	msdosfs_sync,
    126 	msdosfs_vget,
    127 	msdosfs_fhtovp,
    128 	msdosfs_vptofh,
    129 	msdosfs_init,
    130 	msdosfs_reinit,
    131 	msdosfs_done,
    132 	msdosfs_sysctl,
    133 	msdosfs_mountroot,
    134 	msdosfs_checkexp,
    135 	msdosfs_vnodeopv_descs,
    136 };
    137 
    138 static int
    139 update_mp(mp, argp, l)
    140 	struct mount *mp;
    141 	struct msdosfs_args *argp;
    142 	struct lwp *l;
    143 {
    144 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
    145 	int error;
    146 
    147 	pmp->pm_gid = argp->gid;
    148 	pmp->pm_uid = argp->uid;
    149 	pmp->pm_mask = argp->mask & ALLPERMS;
    150 	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
    151 
    152 	/*
    153 	 * GEMDOS knows nothing (yet) about win95
    154 	 */
    155 	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
    156 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
    157 
    158 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
    159 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
    160 	else if (!(pmp->pm_flags &
    161 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
    162 		struct vnode *rootvp;
    163 
    164 		/*
    165 		 * Try to divine whether to support Win'95 long filenames
    166 		 */
    167 		if (FAT32(pmp))
    168 			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
    169 		else {
    170 			if ((error = msdosfs_root(mp, &rootvp, l)) != 0)
    171 				return error;
    172 			pmp->pm_flags |= findwin95(VTODE(rootvp))
    173 				? MSDOSFSMNT_LONGNAME
    174 					: MSDOSFSMNT_SHORTNAME;
    175 			vput(rootvp);
    176 		}
    177 	}
    178 	return 0;
    179 }
    180 
    181 int
    182 msdosfs_mountroot()
    183 {
    184 	struct mount *mp;
    185 	struct lwp *l = curlwp;	/* XXX */
    186 	int error;
    187 	struct msdosfs_args args;
    188 
    189 	if (root_device->dv_class != DV_DISK)
    190 		return (ENODEV);
    191 
    192 	/*
    193 	 * Get vnodes for swapdev and rootdev.
    194 	 */
    195 	if (bdevvp(rootdev, &rootvp))
    196 		panic("msdosfs_mountroot: can't setup rootvp");
    197 
    198 	if ((error = vfs_rootmountalloc(MOUNT_MSDOS, "root_device", &mp))) {
    199 		vrele(rootvp);
    200 		return (error);
    201 	}
    202 
    203 	args.flags = 0;
    204 	args.uid = 0;
    205 	args.gid = 0;
    206 	args.mask = 0777;
    207 
    208 	if ((error = msdosfs_mountfs(rootvp, mp, l, &args)) != 0) {
    209 		mp->mnt_op->vfs_refcount--;
    210 		vfs_unbusy(mp);
    211 		free(mp, M_MOUNT);
    212 		vrele(rootvp);
    213 		return (error);
    214 	}
    215 
    216 	if ((error = update_mp(mp, &args, l)) != 0) {
    217 		(void)msdosfs_unmount(mp, 0, l);
    218 		vfs_unbusy(mp);
    219 		free(mp, M_MOUNT);
    220 		vrele(rootvp);
    221 		return (error);
    222 	}
    223 
    224 	simple_lock(&mountlist_slock);
    225 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    226 	simple_unlock(&mountlist_slock);
    227 	(void)msdosfs_statfs(mp, &mp->mnt_stat, l);
    228 	vfs_unbusy(mp);
    229 	return (0);
    230 }
    231 
    232 /*
    233  * mp - path - addr in user space of mount point (ie /usr or whatever)
    234  * data - addr in user space of mount params including the name of the block
    235  * special file to treat as a filesystem.
    236  */
    237 int
    238 msdosfs_mount(mp, path, data, ndp, l)
    239 	struct mount *mp;
    240 	const char *path;
    241 	void *data;
    242 	struct nameidata *ndp;
    243 	struct lwp *l;
    244 {
    245 	struct vnode *devvp;	  /* vnode for blk device to mount */
    246 	struct msdosfs_args args; /* will hold data from mount request */
    247 	/* msdosfs specific mount control block */
    248 	struct msdosfsmount *pmp = NULL;
    249 	struct proc *p;
    250 	int error, flags;
    251 	mode_t accessmode;
    252 
    253 	p = l->l_proc;
    254 	if (mp->mnt_flag & MNT_GETARGS) {
    255 		pmp = VFSTOMSDOSFS(mp);
    256 		if (pmp == NULL)
    257 			return EIO;
    258 		args.fspec = NULL;
    259 		args.uid = pmp->pm_uid;
    260 		args.gid = pmp->pm_gid;
    261 		args.mask = pmp->pm_mask;
    262 		args.flags = pmp->pm_flags;
    263 		vfs_showexport(mp, &args.export, &pmp->pm_export);
    264 		return copyout(&args, data, sizeof(args));
    265 	}
    266 	error = copyin(data, &args, sizeof(struct msdosfs_args));
    267 	if (error)
    268 		return (error);
    269 	/*
    270 	 * If updating, check whether changing from read-only to
    271 	 * read/write; if there is no device name, that's all we do.
    272 	 */
    273 	if (mp->mnt_flag & MNT_UPDATE) {
    274 		pmp = VFSTOMSDOSFS(mp);
    275 		error = 0;
    276 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
    277 			flags = WRITECLOSE;
    278 			if (mp->mnt_flag & MNT_FORCE)
    279 				flags |= FORCECLOSE;
    280 			error = vflush(mp, NULLVP, flags);
    281 		}
    282 		if (!error && (mp->mnt_flag & MNT_RELOAD))
    283 			/* not yet implemented */
    284 			error = EOPNOTSUPP;
    285 		if (error)
    286 			return (error);
    287 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_WANTRDWR)) {
    288 			/*
    289 			 * If upgrade to read-write by non-root, then verify
    290 			 * that user has necessary permissions on the device.
    291 			 */
    292 			if (p->p_ucred->cr_uid != 0) {
    293 				devvp = pmp->pm_devvp;
    294 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    295 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
    296 						   l->l_proc->p_ucred, l);
    297 				VOP_UNLOCK(devvp, 0);
    298 				if (error)
    299 					return (error);
    300 			}
    301 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
    302 		}
    303 		if (args.fspec == 0) {
    304 #ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
    305 			if (args.flags & MSDOSFSMNT_MNTOPT) {
    306 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
    307 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
    308 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
    309 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
    310 			}
    311 #endif
    312 			/*
    313 			 * Process export requests.
    314 			 */
    315 			return (vfs_export(mp, &pmp->pm_export, &args.export));
    316 		}
    317 	}
    318 	/*
    319 	 * Not an update, or updating the name: look up the name
    320 	 * and verify that it refers to a sensible block device.
    321 	 */
    322 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, l);
    323 	if ((error = namei(ndp)) != 0)
    324 		return (error);
    325 	devvp = ndp->ni_vp;
    326 
    327 	if (devvp->v_type != VBLK) {
    328 		vrele(devvp);
    329 		return (ENOTBLK);
    330 	}
    331 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    332 		vrele(devvp);
    333 		return (ENXIO);
    334 	}
    335 	/*
    336 	 * If mount by non-root, then verify that user has necessary
    337 	 * permissions on the device.
    338 	 */
    339 	if (p->p_ucred->cr_uid != 0) {
    340 		accessmode = VREAD;
    341 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    342 			accessmode |= VWRITE;
    343 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    344 		error = VOP_ACCESS(devvp, accessmode, l->l_proc->p_ucred, l);
    345 		VOP_UNLOCK(devvp, 0);
    346 		if (error) {
    347 			vrele(devvp);
    348 			return (error);
    349 		}
    350 	}
    351 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
    352 		error = msdosfs_mountfs(devvp, mp, l, &args);
    353 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
    354 		pmp = VFSTOMSDOSFS(mp);
    355 #endif
    356 	} else {
    357 		if (devvp != pmp->pm_devvp)
    358 			error = EINVAL;	/* needs translation */
    359 		else
    360 			vrele(devvp);
    361 	}
    362 	if (error) {
    363 		vrele(devvp);
    364 		return (error);
    365 	}
    366 
    367 	if ((error = update_mp(mp, &args, l)) != 0) {
    368 		msdosfs_unmount(mp, MNT_FORCE, l);
    369 		return error;
    370 	}
    371 
    372 #ifdef MSDOSFS_DEBUG
    373 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
    374 #endif
    375 	return set_statfs_info(path, UIO_USERSPACE, args.fspec, UIO_USERSPACE,
    376 	    mp, l);
    377 }
    378 
    379 int
    380 msdosfs_mountfs(devvp, mp, l, argp)
    381 	struct vnode *devvp;
    382 	struct mount *mp;
    383 	struct lwp *l;
    384 	struct msdosfs_args *argp;
    385 {
    386 	struct msdosfsmount *pmp;
    387 	struct buf *bp;
    388 	dev_t dev = devvp->v_rdev;
    389 	struct partinfo dpart;
    390 	union bootsector *bsp;
    391 	struct byte_bpb33 *b33;
    392 	struct byte_bpb50 *b50;
    393 	struct byte_bpb710 *b710;
    394 	u_int8_t SecPerClust;
    395 	int	ronly, error;
    396 	int	bsize = 0, dtype = 0, tmp;
    397 	u_long	dirsperblk;
    398 
    399 	/*
    400 	 * Disallow multiple mounts of the same device.
    401 	 * Disallow mounting of a device that is currently in use
    402 	 * (except for root, which might share swap device for miniroot).
    403 	 * Flush out any old buffers remaining from a previous use.
    404 	 */
    405 	if ((error = vfs_mountedon(devvp)) != 0)
    406 		return (error);
    407 	if (vcount(devvp) > 1 && devvp != rootvp)
    408 		return (EBUSY);
    409 	if ((error = vinvalbuf(devvp, V_SAVE, l->l_proc->p_ucred, l, 0, 0)) != 0)
    410 		return (error);
    411 
    412 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    413 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, l);
    414 	if (error)
    415 		return (error);
    416 
    417 	bp  = NULL; /* both used in error_exit */
    418 	pmp = NULL;
    419 
    420 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
    421 		/*
    422 	 	 * We need the disklabel to calculate the size of a FAT entry
    423 		 * later on. Also make sure the partition contains a filesystem
    424 		 * of type FS_MSDOS. This doesn't work for floppies, so we have
    425 		 * to check for them too.
    426 	 	 *
    427 	 	 * At least some parts of the msdos fs driver seem to assume
    428 		 * that the size of a disk block will always be 512 bytes.
    429 		 * Let's check it...
    430 		 */
    431 		error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, l);
    432 		if (error)
    433 			goto error_exit;
    434 		tmp   = dpart.part->p_fstype;
    435 		dtype = dpart.disklab->d_type;
    436 		bsize = dpart.disklab->d_secsize;
    437 		if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
    438 			error = EINVAL;
    439 			goto error_exit;
    440 		}
    441 	}
    442 
    443 	/*
    444 	 * Read the boot sector of the filesystem, and then check the
    445 	 * boot signature.  If not a dos boot sector then error out.
    446 	 */
    447 	if ((error = bread(devvp, 0, 512, NOCRED, &bp)) != 0)
    448 		goto error_exit;
    449 	bp->b_flags |= B_AGE;
    450 	bsp = (union bootsector *)bp->b_data;
    451 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
    452 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
    453 	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
    454 
    455 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
    456 		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
    457 		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
    458 			error = EINVAL;
    459 			goto error_exit;
    460 		}
    461 	}
    462 
    463 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
    464 	memset(pmp, 0, sizeof *pmp);
    465 	pmp->pm_mountp = mp;
    466 
    467 	/*
    468 	 * Compute several useful quantities from the bpb in the
    469 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
    470 	 * the fields that are different between dos 5 and dos 3.3.
    471 	 */
    472 	SecPerClust = b50->bpbSecPerClust;
    473 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
    474 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
    475 	pmp->pm_FATs = b50->bpbFATs;
    476 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
    477 	pmp->pm_Sectors = getushort(b50->bpbSectors);
    478 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
    479 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
    480 	pmp->pm_Heads = getushort(b50->bpbHeads);
    481 	pmp->pm_Media = b50->bpbMedia;
    482 
    483 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
    484 		/* XXX - We should probably check more values here */
    485     		if (!pmp->pm_BytesPerSec || !SecPerClust
    486 	    		|| pmp->pm_Heads > 255 || pmp->pm_SecPerTrack > 63) {
    487 			error = EINVAL;
    488 			goto error_exit;
    489 		}
    490 	}
    491 
    492 	if (pmp->pm_Sectors == 0) {
    493 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
    494 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
    495 	} else {
    496 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
    497 		pmp->pm_HugeSectors = pmp->pm_Sectors;
    498 	}
    499 	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
    500 	if (pmp->pm_HugeSectors > 0xffffffff / dirsperblk + 1) {
    501 		/*
    502 		 * We cannot deal currently with this size of disk
    503 		 * due to fileid limitations (see msdosfs_getattr and
    504 		 * msdosfs_readdir)
    505 		 */
    506 		error = EINVAL;
    507 		goto error_exit;
    508 	}
    509 
    510 	if (pmp->pm_RootDirEnts == 0) {
    511 		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
    512 		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
    513 		    || pmp->pm_Sectors
    514 		    || pmp->pm_FATsecs
    515 		    || getushort(b710->bpbFSVers)) {
    516 			error = EINVAL;
    517 			goto error_exit;
    518 		}
    519 		pmp->pm_fatmask = FAT32_MASK;
    520 		pmp->pm_fatmult = 4;
    521 		pmp->pm_fatdiv = 1;
    522 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
    523 
    524 		/* mirrorring is enabled if the FATMIRROR bit is not set */
    525 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
    526 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
    527 		else
    528 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
    529 	} else
    530 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
    531 
    532 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
    533 		if (FAT32(pmp)) {
    534 			/*
    535 			 * GEMDOS doesn't know fat32.
    536 			 */
    537 			error = EINVAL;
    538 			goto error_exit;
    539 		}
    540 
    541 		/*
    542 		 * Check a few values (could do some more):
    543 		 * - logical sector size: power of 2, >= block size
    544 		 * - sectors per cluster: power of 2, >= 1
    545 		 * - number of sectors:   >= 1, <= size of partition
    546 		 */
    547 		if ( (SecPerClust == 0)
    548 		  || (SecPerClust & (SecPerClust - 1))
    549 		  || (pmp->pm_BytesPerSec < bsize)
    550 		  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
    551 		  || (pmp->pm_HugeSectors == 0)
    552 		  || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
    553 							> dpart.part->p_size)
    554 		   ) {
    555 			error = EINVAL;
    556 			goto error_exit;
    557 		}
    558 		/*
    559 		 * XXX - Many parts of the msdos fs driver seem to assume that
    560 		 * the number of bytes per logical sector (BytesPerSec) will
    561 		 * always be the same as the number of bytes per disk block
    562 		 * Let's pretend it is.
    563 		 */
    564 		tmp = pmp->pm_BytesPerSec / bsize;
    565 		pmp->pm_BytesPerSec  = bsize;
    566 		pmp->pm_HugeSectors *= tmp;
    567 		pmp->pm_HiddenSects *= tmp;
    568 		pmp->pm_ResSectors  *= tmp;
    569 		pmp->pm_Sectors     *= tmp;
    570 		pmp->pm_FATsecs     *= tmp;
    571 		SecPerClust         *= tmp;
    572 	}
    573 	pmp->pm_fatblk = pmp->pm_ResSectors;
    574 	if (FAT32(pmp)) {
    575 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
    576 		pmp->pm_firstcluster = pmp->pm_fatblk
    577 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
    578 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
    579 	} else {
    580 		pmp->pm_rootdirblk = pmp->pm_fatblk +
    581 			(pmp->pm_FATs * pmp->pm_FATsecs);
    582 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
    583 				       + pmp->pm_BytesPerSec - 1)
    584 			/ pmp->pm_BytesPerSec;/* in sectors */
    585 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
    586 	}
    587 
    588 	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
    589 	    SecPerClust;
    590 	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
    591 	pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
    592 
    593 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
    594 		if (pmp->pm_nmbrofclusters <= (0xff0 - 2)
    595 		      && (dtype == DTYPE_FLOPPY
    596 			  || (dtype == DTYPE_VND
    597 				&& (pmp->pm_Heads == 1 || pmp->pm_Heads == 2)))
    598 		    ) {
    599 			pmp->pm_fatmask = FAT12_MASK;
    600 			pmp->pm_fatmult = 3;
    601 			pmp->pm_fatdiv = 2;
    602 		} else {
    603 			pmp->pm_fatmask = FAT16_MASK;
    604 			pmp->pm_fatmult = 2;
    605 			pmp->pm_fatdiv = 1;
    606 		}
    607 	} else if (pmp->pm_fatmask == 0) {
    608 		if (pmp->pm_maxcluster
    609 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
    610 			/*
    611 			 * This will usually be a floppy disk. This size makes
    612 			 * sure that one fat entry will not be split across
    613 			 * multiple blocks.
    614 			 */
    615 			pmp->pm_fatmask = FAT12_MASK;
    616 			pmp->pm_fatmult = 3;
    617 			pmp->pm_fatdiv = 2;
    618 		} else {
    619 			pmp->pm_fatmask = FAT16_MASK;
    620 			pmp->pm_fatmult = 2;
    621 			pmp->pm_fatdiv = 1;
    622 		}
    623 	}
    624 	if (FAT12(pmp))
    625 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
    626 	else
    627 		pmp->pm_fatblocksize = MAXBSIZE;
    628 
    629 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
    630 	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
    631 
    632 	/*
    633 	 * Compute mask and shift value for isolating cluster relative byte
    634 	 * offsets and cluster numbers from a file offset.
    635 	 */
    636 	pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
    637 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
    638 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
    639 
    640 	/*
    641 	 * Check for valid cluster size
    642 	 * must be a power of 2
    643 	 */
    644 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
    645 		error = EINVAL;
    646 		goto error_exit;
    647 	}
    648 
    649 	/*
    650 	 * Release the bootsector buffer.
    651 	 */
    652 	brelse(bp);
    653 	bp = NULL;
    654 
    655 	/*
    656 	 * Check FSInfo.
    657 	 */
    658 	if (pmp->pm_fsinfo) {
    659 		struct fsinfo *fp;
    660 
    661 		if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0)
    662 			goto error_exit;
    663 		fp = (struct fsinfo *)bp->b_data;
    664 		if (!memcmp(fp->fsisig1, "RRaA", 4)
    665 		    && !memcmp(fp->fsisig2, "rrAa", 4)
    666 		    && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
    667 		    && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
    668 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
    669 		else
    670 			pmp->pm_fsinfo = 0;
    671 		brelse(bp);
    672 		bp = NULL;
    673 	}
    674 
    675 	/*
    676 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?
    677 	 * XXX
    678 	 */
    679 	if (pmp->pm_fsinfo) {
    680 		if (pmp->pm_nxtfree == (u_long)-1)
    681 			pmp->pm_fsinfo = 0;
    682 	}
    683 
    684 	/*
    685 	 * Allocate memory for the bitmap of allocated clusters, and then
    686 	 * fill it in.
    687 	 */
    688 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
    689 				   / N_INUSEBITS)
    690 				  * sizeof(*pmp->pm_inusemap),
    691 				  M_MSDOSFSFAT, M_WAITOK);
    692 
    693 	/*
    694 	 * fillinusemap() needs pm_devvp.
    695 	 */
    696 	pmp->pm_dev = dev;
    697 	pmp->pm_devvp = devvp;
    698 
    699 	/*
    700 	 * Have the inuse map filled in.
    701 	 */
    702 	if ((error = fillinusemap(pmp)) != 0)
    703 		goto error_exit;
    704 
    705 	/*
    706 	 * If they want fat updates to be synchronous then let them suffer
    707 	 * the performance degradation in exchange for the on disk copy of
    708 	 * the fat being correct just about all the time.  I suppose this
    709 	 * would be a good thing to turn on if the kernel is still flakey.
    710 	 */
    711 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
    712 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
    713 
    714 	/*
    715 	 * Finish up.
    716 	 */
    717 	if (ronly)
    718 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
    719 	else
    720 		pmp->pm_fmod = 1;
    721 	mp->mnt_data = pmp;
    722         mp->mnt_stat.f_fsid.val[0] = (long)dev;
    723         mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_MSDOS);
    724 	mp->mnt_flag |= MNT_LOCAL;
    725 	mp->mnt_dev_bshift = pmp->pm_bnshift;
    726 	mp->mnt_fs_bshift = pmp->pm_cnshift;
    727 
    728 #ifdef QUOTA
    729 	/*
    730 	 * If we ever do quotas for DOS filesystems this would be a place
    731 	 * to fill in the info in the msdosfsmount structure. You dolt,
    732 	 * quotas on dos filesystems make no sense because files have no
    733 	 * owners on dos filesystems. of course there is some empty space
    734 	 * in the directory entry where we could put uid's and gid's.
    735 	 */
    736 #endif
    737 	devvp->v_specmountpoint = mp;
    738 
    739 	return (0);
    740 
    741 error_exit:;
    742 	if (bp)
    743 		brelse(bp);
    744 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    745 	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, l);
    746 	VOP_UNLOCK(devvp, 0);
    747 	if (pmp) {
    748 		if (pmp->pm_inusemap)
    749 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
    750 		free(pmp, M_MSDOSFSMNT);
    751 		mp->mnt_data = NULL;
    752 	}
    753 	return (error);
    754 }
    755 
    756 int
    757 msdosfs_start(mp, flags, l)
    758 	struct mount *mp;
    759 	int flags;
    760 	struct lwp *l;
    761 {
    762 
    763 	return (0);
    764 }
    765 
    766 /*
    767  * Unmount the filesystem described by mp.
    768  */
    769 int
    770 msdosfs_unmount(mp, mntflags, l)
    771 	struct mount *mp;
    772 	int mntflags;
    773 	struct lwp *l;
    774 {
    775 	struct msdosfsmount *pmp;
    776 	int error, flags;
    777 
    778 	flags = 0;
    779 	if (mntflags & MNT_FORCE)
    780 		flags |= FORCECLOSE;
    781 #ifdef QUOTA
    782 #endif
    783 	if ((error = vflush(mp, NULLVP, flags)) != 0)
    784 		return (error);
    785 	pmp = VFSTOMSDOSFS(mp);
    786 	if (pmp->pm_devvp->v_type != VBAD)
    787 		pmp->pm_devvp->v_specmountpoint = NULL;
    788 #ifdef MSDOSFS_DEBUG
    789 	{
    790 		struct vnode *vp = pmp->pm_devvp;
    791 
    792 		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
    793 		printf("flag %08x, usecount %d, writecount %ld, holdcnt %ld\n",
    794 		    vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
    795 		printf("id %lu, mount %p, op %p\n",
    796 		    vp->v_id, vp->v_mount, vp->v_op);
    797 		printf("freef %p, freeb %p, mount %p\n",
    798 		    vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
    799 		    vp->v_mount);
    800 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
    801 		    vp->v_cleanblkhd.lh_first,
    802 		    vp->v_dirtyblkhd.lh_first,
    803 		    vp->v_numoutput, vp->v_type);
    804 		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
    805 		    vp->v_socket, vp->v_tag,
    806 		    ((u_int *)vp->v_data)[0],
    807 		    ((u_int *)vp->v_data)[1]);
    808 	}
    809 #endif
    810 	vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
    811 	error = VOP_CLOSE(pmp->pm_devvp,
    812 	    pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, l);
    813 	vput(pmp->pm_devvp);
    814 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
    815 	free(pmp, M_MSDOSFSMNT);
    816 	mp->mnt_data = NULL;
    817 	mp->mnt_flag &= ~MNT_LOCAL;
    818 	return (error);
    819 }
    820 
    821 int
    822 msdosfs_root(mp, vpp, l)
    823 	struct mount *mp;
    824 	struct vnode **vpp;
    825 	struct lwp *l;
    826 {
    827 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
    828 	struct denode *ndep;
    829 	int error;
    830 
    831 #ifdef MSDOSFS_DEBUG
    832 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
    833 #endif
    834 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
    835 		return (error);
    836 	*vpp = DETOV(ndep);
    837 	return (0);
    838 }
    839 
    840 int
    841 msdosfs_quotactl(mp, cmds, uid, arg, l)
    842 	struct mount *mp;
    843 	int cmds;
    844 	uid_t uid;
    845 	caddr_t arg;
    846 	struct lwp *l;
    847 {
    848 
    849 #ifdef QUOTA
    850 	return (EOPNOTSUPP);
    851 #else
    852 	return (EOPNOTSUPP);
    853 #endif
    854 }
    855 
    856 int
    857 msdosfs_statfs(mp, sbp, l)
    858 	struct mount *mp;
    859 	struct statfs *sbp;
    860 	struct lwp *l;
    861 {
    862 	struct msdosfsmount *pmp;
    863 
    864 	pmp = VFSTOMSDOSFS(mp);
    865 #ifdef COMPAT_09
    866 	sbp->f_type = 4;
    867 #else
    868 	sbp->f_type = 0;
    869 #endif
    870 	sbp->f_bsize = pmp->pm_bpcluster;
    871 	sbp->f_iosize = pmp->pm_bpcluster;
    872 	sbp->f_blocks = pmp->pm_nmbrofclusters;
    873 	sbp->f_bfree = pmp->pm_freeclustercount;
    874 	sbp->f_bavail = pmp->pm_freeclustercount;
    875 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
    876 	sbp->f_ffree = 0;	/* what to put in here? */
    877 	copy_statfs_info(sbp, mp);
    878 	return (0);
    879 }
    880 
    881 int
    882 msdosfs_sync(mp, waitfor, cred, l)
    883 	struct mount *mp;
    884 	int waitfor;
    885 	struct ucred *cred;
    886 	struct lwp *l;
    887 {
    888 	struct vnode *vp, *nvp;
    889 	struct denode *dep;
    890 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
    891 	int error, allerror = 0;
    892 
    893 	/*
    894 	 * If we ever switch to not updating all of the fats all the time,
    895 	 * this would be the place to update them from the first one.
    896 	 */
    897 	if (pmp->pm_fmod != 0) {
    898 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
    899 			panic("msdosfs_sync: rofs mod");
    900 		else {
    901 			/* update fats here */
    902 		}
    903 	}
    904 	/*
    905 	 * Write back each (modified) denode.
    906 	 */
    907 	simple_lock(&mntvnode_slock);
    908 loop:
    909 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
    910 		/*
    911 		 * If the vnode that we are about to sync is no longer
    912 		 * assoicated with this mount point, start over.
    913 		 */
    914 		if (vp->v_mount != mp)
    915 			goto loop;
    916 		simple_lock(&vp->v_interlock);
    917 		nvp = vp->v_mntvnodes.le_next;
    918 		dep = VTODE(vp);
    919 		if (waitfor == MNT_LAZY || vp->v_type == VNON ||
    920 		    (((dep->de_flag &
    921 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
    922 		     (LIST_EMPTY(&vp->v_dirtyblkhd) &&
    923 		      vp->v_uobj.uo_npages == 0))) {
    924 			simple_unlock(&vp->v_interlock);
    925 			continue;
    926 		}
    927 		simple_unlock(&mntvnode_slock);
    928 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, l);
    929 		if (error) {
    930 			simple_lock(&mntvnode_slock);
    931 			if (error == ENOENT)
    932 				goto loop;
    933 			continue;
    934 		}
    935 		if ((error = VOP_FSYNC(vp, cred,
    936 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
    937 			allerror = error;
    938 		vput(vp);
    939 		simple_lock(&mntvnode_slock);
    940 	}
    941 	simple_unlock(&mntvnode_slock);
    942 	/*
    943 	 * Force stale file system control information to be flushed.
    944 	 */
    945 	if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
    946 	    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, l)) != 0)
    947 		allerror = error;
    948 #ifdef QUOTA
    949 	/* qsync(mp); */
    950 #endif
    951 	return (allerror);
    952 }
    953 
    954 int
    955 msdosfs_fhtovp(mp, fhp, vpp, l)
    956 	struct mount *mp;
    957 	struct fid *fhp;
    958 	struct vnode **vpp;
    959 	struct lwp *l;
    960 {
    961 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
    962 	struct defid *defhp = (struct defid *) fhp;
    963 	struct denode *dep;
    964 	int error;
    965 
    966 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
    967 	if (error) {
    968 		*vpp = NULLVP;
    969 		return (error);
    970 	}
    971 	*vpp = DETOV(dep);
    972 	return (0);
    973 }
    974 
    975 int
    976 msdosfs_checkexp(mp, nam, exflagsp, credanonp)
    977 	struct mount *mp;
    978 	struct mbuf *nam;
    979 	int *exflagsp;
    980 	struct ucred **credanonp;
    981 {
    982 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
    983 	struct netcred *np;
    984 
    985 	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
    986 	if (np == NULL)
    987 		return (EACCES);
    988 	*exflagsp = np->netc_exflags;
    989 	*credanonp = &np->netc_anon;
    990 	return (0);
    991 }
    992 
    993 int
    994 msdosfs_vptofh(vp, fhp)
    995 	struct vnode *vp;
    996 	struct fid *fhp;
    997 {
    998 	struct denode *dep;
    999 	struct defid *defhp;
   1000 
   1001 	dep = VTODE(vp);
   1002 	defhp = (struct defid *)fhp;
   1003 	defhp->defid_len = sizeof(struct defid);
   1004 	defhp->defid_dirclust = dep->de_dirclust;
   1005 	defhp->defid_dirofs = dep->de_diroffset;
   1006 	/* defhp->defid_gen = dep->de_gen; */
   1007 	return (0);
   1008 }
   1009 
   1010 int
   1011 msdosfs_vget(mp, ino, vpp, l)
   1012 	struct mount *mp;
   1013 	ino_t ino;
   1014 	struct vnode **vpp;
   1015 	struct lwp *l;
   1016 {
   1017 
   1018 	return (EOPNOTSUPP);
   1019 }
   1020 
   1021 int
   1022 msdosfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, l)
   1023 	int *name;
   1024 	u_int namelen;
   1025 	void *oldp;
   1026 	size_t *oldlenp;
   1027 	void *newp;
   1028 	size_t newlen;
   1029 	struct lwp *l;
   1030 {
   1031 	return (EOPNOTSUPP);
   1032 }
   1033