Home | History | Annotate | Line # | Download | only in msdosfs
msdosfs_vfsops.c revision 1.105
      1 /*	$NetBSD: msdosfs_vfsops.c,v 1.105 2014/03/17 09:35:59 hannken 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.105 2014/03/17 09:35:59 hannken Exp $");
     52 
     53 #if defined(_KERNEL_OPT)
     54 #include "opt_compat_netbsd.h"
     55 #endif
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/sysctl.h>
     60 #include <sys/namei.h>
     61 #include <sys/proc.h>
     62 #include <sys/kernel.h>
     63 #include <sys/vnode.h>
     64 #include <miscfs/genfs/genfs.h>
     65 #include <miscfs/specfs/specdev.h> /* XXX */	/* defines v_rdev */
     66 #include <sys/mount.h>
     67 #include <sys/buf.h>
     68 #include <sys/file.h>
     69 #include <sys/device.h>
     70 #include <sys/disklabel.h>
     71 #include <sys/disk.h>
     72 #include <sys/fstrans.h>
     73 #include <sys/ioctl.h>
     74 #include <sys/malloc.h>
     75 #include <sys/dirent.h>
     76 #include <sys/stat.h>
     77 #include <sys/conf.h>
     78 #include <sys/kauth.h>
     79 #include <sys/module.h>
     80 
     81 #include <fs/msdosfs/bpb.h>
     82 #include <fs/msdosfs/bootsect.h>
     83 #include <fs/msdosfs/direntry.h>
     84 #include <fs/msdosfs/denode.h>
     85 #include <fs/msdosfs/msdosfsmount.h>
     86 #include <fs/msdosfs/fat.h>
     87 
     88 MODULE(MODULE_CLASS_VFS, msdos, NULL);
     89 
     90 #ifdef MSDOSFS_DEBUG
     91 #define DPRINTF(a) uprintf a
     92 #else
     93 #define DPRINTF(a)
     94 #endif
     95 
     96 #define MSDOSFS_NAMEMAX(pmp) \
     97 	(pmp)->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12
     98 
     99 VFS_PROTOS(msdosfs);
    100 
    101 int msdosfs_mountfs(struct vnode *, struct mount *, struct lwp *,
    102     struct msdosfs_args *);
    103 
    104 static int update_mp(struct mount *, struct msdosfs_args *);
    105 
    106 MALLOC_JUSTDEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOS FS mount structure");
    107 MALLOC_JUSTDEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOS FS FAT table");
    108 MALLOC_JUSTDEFINE(M_MSDOSFSTMP, "MSDOSFS temp", "MSDOS FS temp. structures");
    109 
    110 #define ROOTNAME "root_device"
    111 
    112 static struct sysctllog *msdosfs_sysctl_log;
    113 
    114 extern const struct vnodeopv_desc msdosfs_vnodeop_opv_desc;
    115 
    116 const struct vnodeopv_desc * const msdosfs_vnodeopv_descs[] = {
    117 	&msdosfs_vnodeop_opv_desc,
    118 	NULL,
    119 };
    120 
    121 struct vfsops msdosfs_vfsops = {
    122 	MOUNT_MSDOS,
    123 	sizeof (struct msdosfs_args),
    124 	msdosfs_mount,
    125 	msdosfs_start,
    126 	msdosfs_unmount,
    127 	msdosfs_root,
    128 	(void *)eopnotsupp,		/* vfs_quotactl */
    129 	msdosfs_statvfs,
    130 	msdosfs_sync,
    131 	msdosfs_vget,
    132 	msdosfs_fhtovp,
    133 	msdosfs_vptofh,
    134 	msdosfs_init,
    135 	msdosfs_reinit,
    136 	msdosfs_done,
    137 	msdosfs_mountroot,
    138 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    139 	vfs_stdextattrctl,
    140 	msdosfs_suspendctl,
    141 	genfs_renamelock_enter,
    142 	genfs_renamelock_exit,
    143 	(void *)eopnotsupp,
    144 	msdosfs_vnodeopv_descs,
    145 	0,
    146 	{ NULL, NULL },
    147 };
    148 
    149 static int
    150 msdos_modcmd(modcmd_t cmd, void *arg)
    151 {
    152 	int error;
    153 
    154 	switch (cmd) {
    155 	case MODULE_CMD_INIT:
    156 		error = vfs_attach(&msdosfs_vfsops);
    157 		if (error != 0)
    158 			break;
    159 		sysctl_createv(&msdosfs_sysctl_log, 0, NULL, NULL,
    160 			       CTLFLAG_PERMANENT,
    161 			       CTLTYPE_NODE, "msdosfs",
    162 			       SYSCTL_DESCR("MS-DOS file system"),
    163 			       NULL, 0, NULL, 0,
    164 			       CTL_VFS, 4, CTL_EOL);
    165 		/*
    166 		 * XXX the "4" above could be dynamic, thereby eliminating one
    167 		 * more instance of the "number to vfs" mapping problem, but
    168 		 * "4" is the order as taken from sys/mount.h
    169 		 */
    170 		break;
    171 	case MODULE_CMD_FINI:
    172 		error = vfs_detach(&msdosfs_vfsops);
    173 		if (error != 0)
    174 			break;
    175 		sysctl_teardown(&msdosfs_sysctl_log);
    176 		break;
    177 	default:
    178 		error = ENOTTY;
    179 		break;
    180 	}
    181 
    182 	return (error);
    183 }
    184 
    185 static int
    186 update_mp(struct mount *mp, struct msdosfs_args *argp)
    187 {
    188 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
    189 	int error;
    190 
    191 	pmp->pm_gid = argp->gid;
    192 	pmp->pm_uid = argp->uid;
    193 	pmp->pm_mask = argp->mask & ALLPERMS;
    194 	pmp->pm_dirmask = argp->dirmask & ALLPERMS;
    195 	pmp->pm_gmtoff = argp->gmtoff;
    196 	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
    197 
    198 	/*
    199 	 * GEMDOS knows nothing about win95 long filenames
    200 	 */
    201 	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
    202 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
    203 
    204 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
    205 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
    206 	else if (!(pmp->pm_flags &
    207 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
    208 		struct vnode *rtvp;
    209 
    210 		/*
    211 		 * Try to divine whether to support Win'95 long filenames
    212 		 */
    213 		if (FAT32(pmp))
    214 			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
    215 		else {
    216 			if ((error = msdosfs_root(mp, &rtvp)) != 0)
    217 				return error;
    218 			pmp->pm_flags |= findwin95(VTODE(rtvp))
    219 				? MSDOSFSMNT_LONGNAME
    220 					: MSDOSFSMNT_SHORTNAME;
    221 			vput(rtvp);
    222 		}
    223 	}
    224 
    225 	mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
    226 
    227 	return 0;
    228 }
    229 
    230 int
    231 msdosfs_mountroot(void)
    232 {
    233 	struct mount *mp;
    234 	struct lwp *l = curlwp;	/* XXX */
    235 	int error;
    236 	struct msdosfs_args args;
    237 
    238 	if (device_class(root_device) != DV_DISK)
    239 		return (ENODEV);
    240 
    241 	if ((error = vfs_rootmountalloc(MOUNT_MSDOS, "root_device", &mp))) {
    242 		vrele(rootvp);
    243 		return (error);
    244 	}
    245 
    246 	args.flags = MSDOSFSMNT_VERSIONED;
    247 	args.uid = 0;
    248 	args.gid = 0;
    249 	args.mask = 0777;
    250 	args.version = MSDOSFSMNT_VERSION;
    251 	args.dirmask = 0777;
    252 
    253 	if ((error = msdosfs_mountfs(rootvp, mp, l, &args)) != 0) {
    254 		vfs_unbusy(mp, false, NULL);
    255 		vfs_destroy(mp);
    256 		return (error);
    257 	}
    258 
    259 	if ((error = update_mp(mp, &args)) != 0) {
    260 		(void)msdosfs_unmount(mp, 0);
    261 		vfs_unbusy(mp, false, NULL);
    262 		vfs_destroy(mp);
    263 		vrele(rootvp);
    264 		return (error);
    265 	}
    266 
    267 	mountlist_append(mp);
    268 	(void)msdosfs_statvfs(mp, &mp->mnt_stat);
    269 	vfs_unbusy(mp, false, NULL);
    270 	return (0);
    271 }
    272 
    273 /*
    274  * mp - path - addr in user space of mount point (ie /usr or whatever)
    275  * data - addr in user space of mount params including the name of the block
    276  * special file to treat as a filesystem.
    277  */
    278 int
    279 msdosfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    280 {
    281 	struct lwp *l = curlwp;
    282 	struct vnode *devvp;	  /* vnode for blk device to mount */
    283 	struct msdosfs_args *args = data; /* holds data from mount request */
    284 	/* msdosfs specific mount control block */
    285 	struct msdosfsmount *pmp = NULL;
    286 	int error, flags;
    287 	mode_t accessmode;
    288 
    289 	if (*data_len < sizeof *args)
    290 		return EINVAL;
    291 
    292 	if (mp->mnt_flag & MNT_GETARGS) {
    293 		pmp = VFSTOMSDOSFS(mp);
    294 		if (pmp == NULL)
    295 			return EIO;
    296 		args->fspec = NULL;
    297 		args->uid = pmp->pm_uid;
    298 		args->gid = pmp->pm_gid;
    299 		args->mask = pmp->pm_mask;
    300 		args->flags = pmp->pm_flags;
    301 		args->version = MSDOSFSMNT_VERSION;
    302 		args->dirmask = pmp->pm_dirmask;
    303 		args->gmtoff = pmp->pm_gmtoff;
    304 		*data_len = sizeof *args;
    305 		return 0;
    306 	}
    307 
    308 	/*
    309 	 * If not versioned (i.e. using old mount_msdos(8)), fill in
    310 	 * the additional structure items with suitable defaults.
    311 	 */
    312 	if ((args->flags & MSDOSFSMNT_VERSIONED) == 0) {
    313 		args->version = 1;
    314 		args->dirmask = args->mask;
    315 	}
    316 
    317 	/*
    318 	 * Reset GMT offset for pre-v3 mount structure args.
    319 	 */
    320 	if (args->version < 3)
    321 		args->gmtoff = 0;
    322 
    323 	/*
    324 	 * If updating, check whether changing from read-only to
    325 	 * read/write; if there is no device name, that's all we do.
    326 	 */
    327 	if (mp->mnt_flag & MNT_UPDATE) {
    328 		pmp = VFSTOMSDOSFS(mp);
    329 		error = 0;
    330 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
    331 		    (mp->mnt_flag & MNT_RDONLY)) {
    332 			flags = WRITECLOSE;
    333 			if (mp->mnt_flag & MNT_FORCE)
    334 				flags |= FORCECLOSE;
    335 			error = vflush(mp, NULLVP, flags);
    336 		}
    337 		if (!error && (mp->mnt_flag & MNT_RELOAD))
    338 			/* not yet implemented */
    339 			error = EOPNOTSUPP;
    340 		if (error) {
    341 			DPRINTF(("vflush %d\n", error));
    342 			return (error);
    343 		}
    344 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
    345 		    (mp->mnt_iflag & IMNT_WANTRDWR)) {
    346 			/*
    347 			 * If upgrade to read-write by non-root, then verify
    348 			 * that user has necessary permissions on the device.
    349 			 *
    350 			 * Permission to update a mount is checked higher, so
    351 			 * here we presume updating the mount is okay (for
    352 			 * example, as far as securelevel goes) which leaves us
    353 			 * with the normal check.
    354 			 */
    355 			devvp = pmp->pm_devvp;
    356 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    357 			error = kauth_authorize_system(l->l_cred,
    358 			    KAUTH_SYSTEM_MOUNT, KAUTH_REQ_SYSTEM_MOUNT_DEVICE,
    359 			    mp, devvp, KAUTH_ARG(VREAD | VWRITE));
    360 			VOP_UNLOCK(devvp);
    361 			DPRINTF(("KAUTH_REQ_SYSTEM_MOUNT_DEVICE %d\n", error));
    362 			if (error)
    363 				return (error);
    364 
    365 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
    366 		}
    367 		if (args->fspec == NULL) {
    368 			DPRINTF(("missing fspec\n"));
    369 			return EINVAL;
    370 		}
    371 	}
    372 	/*
    373 	 * Not an update, or updating the name: look up the name
    374 	 * and verify that it refers to a sensible block device.
    375 	 */
    376 	error = namei_simple_user(args->fspec,
    377 				NSM_FOLLOW_NOEMULROOT, &devvp);
    378 	if (error != 0) {
    379 		DPRINTF(("namei %d\n", error));
    380 		return (error);
    381 	}
    382 
    383 	if (devvp->v_type != VBLK) {
    384 		DPRINTF(("not block\n"));
    385 		vrele(devvp);
    386 		return (ENOTBLK);
    387 	}
    388 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
    389 		DPRINTF(("no block switch\n"));
    390 		vrele(devvp);
    391 		return (ENXIO);
    392 	}
    393 	/*
    394 	 * If mount by non-root, then verify that user has necessary
    395 	 * permissions on the device.
    396 	 */
    397 	accessmode = VREAD;
    398 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
    399 		accessmode |= VWRITE;
    400 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    401 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    402 	    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(accessmode));
    403 	VOP_UNLOCK(devvp);
    404 	if (error) {
    405 		DPRINTF(("KAUTH_REQ_SYSTEM_MOUNT_DEVICE %d\n", error));
    406 		vrele(devvp);
    407 		return (error);
    408 	}
    409 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
    410 		int xflags;
    411 
    412 		if (mp->mnt_flag & MNT_RDONLY)
    413 			xflags = FREAD;
    414 		else
    415 			xflags = FREAD|FWRITE;
    416 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    417 		error = VOP_OPEN(devvp, xflags, FSCRED);
    418 		VOP_UNLOCK(devvp);
    419 		if (error) {
    420 			DPRINTF(("VOP_OPEN %d\n", error));
    421 			goto fail;
    422 		}
    423 		error = msdosfs_mountfs(devvp, mp, l, args);
    424 		if (error) {
    425 			DPRINTF(("msdosfs_mountfs %d\n", error));
    426 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
    427 			(void) VOP_CLOSE(devvp, xflags, NOCRED);
    428 			VOP_UNLOCK(devvp);
    429 			goto fail;
    430 		}
    431 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
    432 		pmp = VFSTOMSDOSFS(mp);
    433 #endif
    434 	} else {
    435 		vrele(devvp);
    436 		if (devvp != pmp->pm_devvp) {
    437 			DPRINTF(("devvp %p pmp %p\n",
    438 			    devvp, pmp->pm_devvp));
    439 			return (EINVAL);	/* needs translation */
    440 		}
    441 	}
    442 	if ((error = update_mp(mp, args)) != 0) {
    443 		msdosfs_unmount(mp, MNT_FORCE);
    444 		DPRINTF(("update_mp %d\n", error));
    445 		return error;
    446 	}
    447 
    448 #ifdef MSDOSFS_DEBUG
    449 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
    450 #endif
    451 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
    452 	    mp->mnt_op->vfs_name, mp, l);
    453 
    454 fail:
    455 	vrele(devvp);
    456 	return (error);
    457 }
    458 
    459 int
    460 msdosfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l, struct msdosfs_args *argp)
    461 {
    462 	struct msdosfsmount *pmp;
    463 	struct buf *bp;
    464 	dev_t dev = devvp->v_rdev;
    465 	union bootsector *bsp;
    466 	struct byte_bpb33 *b33;
    467 	struct byte_bpb50 *b50;
    468 	struct byte_bpb710 *b710;
    469 	uint8_t SecPerClust;
    470 	int	ronly, error, tmp;
    471 	int	bsize;
    472 	uint64_t psize;
    473 	unsigned secsize;
    474 
    475 	/* Flush out any old buffers remaining from a previous use. */
    476 	if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
    477 		return (error);
    478 
    479 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
    480 
    481 	bp  = NULL; /* both used in error_exit */
    482 	pmp = NULL;
    483 
    484 	error = fstrans_mount(mp);
    485 	if (error)
    486 		goto error_exit;
    487 
    488 	error = getdisksize(devvp, &psize, &secsize);
    489 	if (error) {
    490 		if (argp->flags & MSDOSFSMNT_GEMDOSFS)
    491 			goto error_exit;
    492 
    493 		/* ok, so it failed.  we most likely don't need the info */
    494 		secsize = DEV_BSIZE;
    495 		psize = 0;
    496 		error = 0;
    497 	}
    498 
    499 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
    500 		bsize = secsize;
    501 		if (bsize != 512) {
    502 			DPRINTF(("Invalid block bsize %d for GEMDOS\n", bsize));
    503 			error = EINVAL;
    504 			goto error_exit;
    505 		}
    506 	} else
    507 		bsize = 0;
    508 
    509 	/*
    510 	 * Read the boot sector of the filesystem, and then check the
    511 	 * boot signature.  If not a dos boot sector then error out.
    512 	 */
    513 	if ((error = bread(devvp, 0, secsize, NOCRED, 0, &bp)) != 0)
    514 		goto error_exit;
    515 	bsp = (union bootsector *)bp->b_data;
    516 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
    517 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
    518 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
    519 
    520 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
    521 		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
    522 		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
    523 			DPRINTF(("bootsig0 %d bootsig1 %d\n",
    524 			    bsp->bs50.bsBootSectSig0,
    525 			    bsp->bs50.bsBootSectSig1));
    526 			error = EINVAL;
    527 			goto error_exit;
    528 		}
    529 	}
    530 
    531 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
    532 	memset(pmp, 0, sizeof *pmp);
    533 	pmp->pm_mountp = mp;
    534 
    535 	/*
    536 	 * Compute several useful quantities from the bpb in the
    537 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
    538 	 * the fields that are different between dos 5 and dos 3.3.
    539 	 */
    540 	SecPerClust = b50->bpbSecPerClust;
    541 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
    542 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
    543 	pmp->pm_FATs = b50->bpbFATs;
    544 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
    545 	pmp->pm_Sectors = getushort(b50->bpbSectors);
    546 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
    547 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
    548 	pmp->pm_Heads = getushort(b50->bpbHeads);
    549 	pmp->pm_Media = b50->bpbMedia;
    550 
    551 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
    552 		/* XXX - We should probably check more values here */
    553     		if (!pmp->pm_BytesPerSec || !SecPerClust
    554 	    		|| pmp->pm_SecPerTrack > 63) {
    555 			DPRINTF(("bytespersec %d secperclust %d "
    556 			    "secpertrack %d\n",
    557 			    pmp->pm_BytesPerSec, SecPerClust,
    558 			    pmp->pm_SecPerTrack));
    559 			error = EINVAL;
    560 			goto error_exit;
    561 		}
    562 	}
    563 
    564 	if (pmp->pm_Sectors == 0) {
    565 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
    566 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
    567 	} else {
    568 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
    569 		pmp->pm_HugeSectors = pmp->pm_Sectors;
    570 	}
    571 
    572 	if (pmp->pm_RootDirEnts == 0) {
    573 		unsigned short vers = getushort(b710->bpbFSVers);
    574 		/*
    575 		 * Some say that bsBootSectSig[23] must be zero, but
    576 		 * Windows does not require this and some digital cameras
    577 		 * do not set these to zero.  Therefore, do not insist.
    578 		 */
    579 		if (pmp->pm_Sectors || pmp->pm_FATsecs || vers) {
    580 			DPRINTF(("sectors %d fatsecs %lu vers %d\n",
    581 			    pmp->pm_Sectors, pmp->pm_FATsecs, vers));
    582 			error = EINVAL;
    583 			goto error_exit;
    584 		}
    585 		pmp->pm_fatmask = FAT32_MASK;
    586 		pmp->pm_fatmult = 4;
    587 		pmp->pm_fatdiv = 1;
    588 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
    589 
    590 		/* mirrorring is enabled if the FATMIRROR bit is not set */
    591 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
    592 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
    593 		else
    594 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
    595 	} else
    596 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
    597 
    598 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
    599 		if (FAT32(pmp)) {
    600 			DPRINTF(("FAT32 for GEMDOS\n"));
    601 			/*
    602 			 * GEMDOS doesn't know FAT32.
    603 			 */
    604 			error = EINVAL;
    605 			goto error_exit;
    606 		}
    607 
    608 		/*
    609 		 * Check a few values (could do some more):
    610 		 * - logical sector size: power of 2, >= block size
    611 		 * - sectors per cluster: power of 2, >= 1
    612 		 * - number of sectors:   >= 1, <= size of partition
    613 		 */
    614 		if ( (SecPerClust == 0)
    615 		  || (SecPerClust & (SecPerClust - 1))
    616 		  || (pmp->pm_BytesPerSec < bsize)
    617 		  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
    618 		  || (pmp->pm_HugeSectors == 0)
    619 		  || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
    620 		      > psize)) {
    621 			DPRINTF(("consistency checks for GEMDOS\n"));
    622 			error = EINVAL;
    623 			goto error_exit;
    624 		}
    625 		/*
    626 		 * XXX - Many parts of the msdosfs driver seem to assume that
    627 		 * the number of bytes per logical sector (BytesPerSec) will
    628 		 * always be the same as the number of bytes per disk block
    629 		 * Let's pretend it is.
    630 		 */
    631 		tmp = pmp->pm_BytesPerSec / bsize;
    632 		pmp->pm_BytesPerSec  = bsize;
    633 		pmp->pm_HugeSectors *= tmp;
    634 		pmp->pm_HiddenSects *= tmp;
    635 		pmp->pm_ResSectors  *= tmp;
    636 		pmp->pm_Sectors     *= tmp;
    637 		pmp->pm_FATsecs     *= tmp;
    638 		SecPerClust         *= tmp;
    639 	}
    640 
    641 	/* Check that fs has nonzero FAT size */
    642 	if (pmp->pm_FATsecs == 0) {
    643 		DPRINTF(("FATsecs is 0\n"));
    644 		error = EINVAL;
    645 		goto error_exit;
    646 	}
    647 
    648 	pmp->pm_fatblk = pmp->pm_ResSectors;
    649 	if (FAT32(pmp)) {
    650 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
    651 		pmp->pm_firstcluster = pmp->pm_fatblk
    652 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
    653 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
    654 	} else {
    655 		pmp->pm_rootdirblk = pmp->pm_fatblk +
    656 			(pmp->pm_FATs * pmp->pm_FATsecs);
    657 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
    658 				       + pmp->pm_BytesPerSec - 1)
    659 			/ pmp->pm_BytesPerSec;/* in sectors */
    660 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
    661 	}
    662 
    663 	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
    664 	    SecPerClust;
    665 	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
    666 	pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
    667 
    668 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
    669 		if (pmp->pm_nmbrofclusters <= (0xff0 - 2)) {
    670 			pmp->pm_fatmask = FAT12_MASK;
    671 			pmp->pm_fatmult = 3;
    672 			pmp->pm_fatdiv = 2;
    673 		} else {
    674 			pmp->pm_fatmask = FAT16_MASK;
    675 			pmp->pm_fatmult = 2;
    676 			pmp->pm_fatdiv = 1;
    677 		}
    678 	} else if (pmp->pm_fatmask == 0) {
    679 		if (pmp->pm_maxcluster
    680 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
    681 			/*
    682 			 * This will usually be a floppy disk. This size makes
    683 			 * sure that one FAT entry will not be split across
    684 			 * multiple blocks.
    685 			 */
    686 			pmp->pm_fatmask = FAT12_MASK;
    687 			pmp->pm_fatmult = 3;
    688 			pmp->pm_fatdiv = 2;
    689 		} else {
    690 			pmp->pm_fatmask = FAT16_MASK;
    691 			pmp->pm_fatmult = 2;
    692 			pmp->pm_fatdiv = 1;
    693 		}
    694 	}
    695 	if (FAT12(pmp))
    696 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
    697 	else
    698 		pmp->pm_fatblocksize = MAXBSIZE;
    699 
    700 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
    701 	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
    702 
    703 	/*
    704 	 * Compute mask and shift value for isolating cluster relative byte
    705 	 * offsets and cluster numbers from a file offset.
    706 	 */
    707 	pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
    708 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
    709 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
    710 
    711 	/*
    712 	 * Check for valid cluster size
    713 	 * must be a power of 2
    714 	 */
    715 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
    716 		DPRINTF(("bpcluster %lu cnshift %lu\n",
    717 		    pmp->pm_bpcluster, pmp->pm_cnshift));
    718 		error = EINVAL;
    719 		goto error_exit;
    720 	}
    721 
    722 	/*
    723 	 * Cluster size must be within limit of MAXBSIZE.
    724 	 * Many FAT filesystems will not have clusters larger than
    725 	 * 32KiB due to limits in Windows versions before Vista.
    726 	 */
    727 	if (pmp->pm_bpcluster > MAXBSIZE) {
    728 		DPRINTF(("bpcluster %lu > MAXBSIZE %d\n",
    729 		    pmp->pm_bpcluster, MAXBSIZE));
    730 		error = EINVAL;
    731 		goto error_exit;
    732 	}
    733 
    734 	/*
    735 	 * Release the bootsector buffer.
    736 	 */
    737 	brelse(bp, BC_AGE);
    738 	bp = NULL;
    739 
    740 	/*
    741 	 * Check FSInfo.
    742 	 */
    743 	if (pmp->pm_fsinfo) {
    744 		struct fsinfo *fp;
    745 
    746 		/*
    747 		 * XXX	If the fsinfo block is stored on media with
    748 		 *	2KB or larger sectors, is the fsinfo structure
    749 		 *	padded at the end or in the middle?
    750 		 */
    751 		if ((error = bread(devvp, de_bn2kb(pmp, pmp->pm_fsinfo),
    752 		    pmp->pm_BytesPerSec, NOCRED, 0, &bp)) != 0)
    753 			goto error_exit;
    754 		fp = (struct fsinfo *)bp->b_data;
    755 		if (!memcmp(fp->fsisig1, "RRaA", 4)
    756 		    && !memcmp(fp->fsisig2, "rrAa", 4)
    757 		    && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
    758 		    && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
    759 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
    760 		else
    761 			pmp->pm_fsinfo = 0;
    762 		brelse(bp, 0);
    763 		bp = NULL;
    764 	}
    765 
    766 	/*
    767 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?
    768 	 * XXX
    769 	 */
    770 	if (pmp->pm_fsinfo) {
    771 		if ((pmp->pm_nxtfree == 0xffffffffUL) ||
    772 		    (pmp->pm_nxtfree > pmp->pm_maxcluster))
    773 			pmp->pm_fsinfo = 0;
    774 	}
    775 
    776 	/*
    777 	 * Allocate memory for the bitmap of allocated clusters, and then
    778 	 * fill it in.
    779 	 */
    780 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS)
    781 				   / N_INUSEBITS)
    782 				  * sizeof(*pmp->pm_inusemap),
    783 				  M_MSDOSFSFAT, M_WAITOK);
    784 
    785 	/*
    786 	 * fillinusemap() needs pm_devvp.
    787 	 */
    788 	pmp->pm_dev = dev;
    789 	pmp->pm_devvp = devvp;
    790 
    791 	/*
    792 	 * Have the inuse map filled in.
    793 	 */
    794 	if ((error = fillinusemap(pmp)) != 0) {
    795 		DPRINTF(("fillinusemap %d\n", error));
    796 		goto error_exit;
    797 	}
    798 
    799 	/*
    800 	 * If they want FAT updates to be synchronous then let them suffer
    801 	 * the performance degradation in exchange for the on disk copy of
    802 	 * the FAT being correct just about all the time.  I suppose this
    803 	 * would be a good thing to turn on if the kernel is still flakey.
    804 	 */
    805 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
    806 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
    807 
    808 	/*
    809 	 * Finish up.
    810 	 */
    811 	if (ronly)
    812 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
    813 	else
    814 		pmp->pm_fmod = 1;
    815 	mp->mnt_data = pmp;
    816 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
    817 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
    818 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
    819 	mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
    820 	mp->mnt_flag |= MNT_LOCAL;
    821 	mp->mnt_dev_bshift = pmp->pm_bnshift;
    822 	mp->mnt_fs_bshift = pmp->pm_cnshift;
    823 
    824 	/*
    825 	 * If we ever do quotas for DOS filesystems this would be a place
    826 	 * to fill in the info in the msdosfsmount structure. You dolt,
    827 	 * quotas on dos filesystems make no sense because files have no
    828 	 * owners on dos filesystems. of course there is some empty space
    829 	 * in the directory entry where we could put uid's and gid's.
    830 	 */
    831 
    832 	spec_node_setmountedfs(devvp, mp);
    833 
    834 	return (0);
    835 
    836 error_exit:
    837 	fstrans_unmount(mp);
    838 	if (bp)
    839 		brelse(bp, BC_AGE);
    840 	if (pmp) {
    841 		if (pmp->pm_inusemap)
    842 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
    843 		free(pmp, M_MSDOSFSMNT);
    844 		mp->mnt_data = NULL;
    845 	}
    846 	return (error);
    847 }
    848 
    849 int
    850 msdosfs_start(struct mount *mp, int flags)
    851 {
    852 
    853 	return (0);
    854 }
    855 
    856 /*
    857  * Unmount the filesystem described by mp.
    858  */
    859 int
    860 msdosfs_unmount(struct mount *mp, int mntflags)
    861 {
    862 	struct msdosfsmount *pmp;
    863 	int error, flags;
    864 
    865 	flags = 0;
    866 	if (mntflags & MNT_FORCE)
    867 		flags |= FORCECLOSE;
    868 	if ((error = vflush(mp, NULLVP, flags)) != 0)
    869 		return (error);
    870 	pmp = VFSTOMSDOSFS(mp);
    871 	if (pmp->pm_devvp->v_type != VBAD)
    872 		spec_node_setmountedfs(pmp->pm_devvp, NULL);
    873 #ifdef MSDOSFS_DEBUG
    874 	{
    875 		struct vnode *vp = pmp->pm_devvp;
    876 
    877 		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
    878 		printf("flag %08x, usecount %d, writecount %d, holdcnt %d\n",
    879 		    vp->v_vflag | vp->v_iflag | vp->v_uflag, vp->v_usecount,
    880 		    vp->v_writecount, vp->v_holdcnt);
    881 		printf("mount %p, op %p\n",
    882 		    vp->v_mount, vp->v_op);
    883 		printf("freef %p, freeb %p, mount %p\n",
    884 		    vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
    885 		    vp->v_mount);
    886 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
    887 		    vp->v_cleanblkhd.lh_first,
    888 		    vp->v_dirtyblkhd.lh_first,
    889 		    vp->v_numoutput, vp->v_type);
    890 		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
    891 		    vp->v_socket, vp->v_tag,
    892 		    ((u_int *)vp->v_data)[0],
    893 		    ((u_int *)vp->v_data)[1]);
    894 	}
    895 #endif
    896 	vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
    897 	(void) VOP_CLOSE(pmp->pm_devvp,
    898 	    pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED);
    899 	vput(pmp->pm_devvp);
    900 	msdosfs_fh_destroy(pmp);
    901 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
    902 	free(pmp, M_MSDOSFSMNT);
    903 	mp->mnt_data = NULL;
    904 	mp->mnt_flag &= ~MNT_LOCAL;
    905 	fstrans_unmount(mp);
    906 	return (0);
    907 }
    908 
    909 int
    910 msdosfs_root(struct mount *mp, struct vnode **vpp)
    911 {
    912 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
    913 	struct denode *ndep;
    914 	int error;
    915 
    916 #ifdef MSDOSFS_DEBUG
    917 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
    918 #endif
    919 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
    920 		return (error);
    921 	*vpp = DETOV(ndep);
    922 	return (0);
    923 }
    924 
    925 int
    926 msdosfs_statvfs(struct mount *mp, struct statvfs *sbp)
    927 {
    928 	struct msdosfsmount *pmp;
    929 
    930 	pmp = VFSTOMSDOSFS(mp);
    931 	sbp->f_bsize = pmp->pm_bpcluster;
    932 	sbp->f_frsize = sbp->f_bsize;
    933 	sbp->f_iosize = pmp->pm_bpcluster;
    934 	sbp->f_blocks = pmp->pm_nmbrofclusters;
    935 	sbp->f_bfree = pmp->pm_freeclustercount;
    936 	sbp->f_bavail = pmp->pm_freeclustercount;
    937 	sbp->f_bresvd = 0;
    938 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
    939 	sbp->f_ffree = 0;	/* what to put in here? */
    940 	sbp->f_favail = 0;	/* what to put in here? */
    941 	sbp->f_fresvd = 0;
    942 	copy_statvfs_info(sbp, mp);
    943 	return (0);
    944 }
    945 
    946 int
    947 msdosfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
    948 {
    949 	struct vnode *vp;
    950 	struct vnode_iterator *marker;
    951 	struct denode *dep;
    952 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
    953 	int error, allerror = 0;
    954 
    955 	/*
    956 	 * If we ever switch to not updating all of the FATs all the time,
    957 	 * this would be the place to update them from the first one.
    958 	 */
    959 	if (pmp->pm_fmod != 0) {
    960 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
    961 			panic("msdosfs_sync: rofs mod");
    962 		else {
    963 			/* update FATs here */
    964 		}
    965 	}
    966 	fstrans_start(mp, FSTRANS_SHARED);
    967 	/*
    968 	 * Write back each (modified) denode.
    969 	 */
    970 	vfs_vnode_iterator_init(mp, &marker);
    971 	while (vfs_vnode_iterator_next(marker, &vp)) {
    972 		error = vn_lock(vp, LK_EXCLUSIVE);
    973 		if (error) {
    974 			vrele(vp);
    975 			continue;
    976 		}
    977 		dep = VTODE(vp);
    978 		if (waitfor == MNT_LAZY || vp->v_type == VNON ||
    979 		    dep == NULL || (((dep->de_flag &
    980 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
    981 		     (LIST_EMPTY(&vp->v_dirtyblkhd) &&
    982 		      UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
    983 			vput(vp);
    984 			continue;
    985 		}
    986 		if ((error = VOP_FSYNC(vp, cred,
    987 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
    988 			allerror = error;
    989 		vput(vp);
    990 	}
    991 	vfs_vnode_iterator_destroy(marker);
    992 
    993 	/*
    994 	 * Force stale file system control information to be flushed.
    995 	 */
    996 	if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
    997 	    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
    998 		allerror = error;
    999 	fstrans_done(mp);
   1000 	return (allerror);
   1001 }
   1002 
   1003 int
   1004 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
   1005 {
   1006 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
   1007 	struct defid defh;
   1008 	struct denode *dep;
   1009 	uint32_t gen;
   1010 	int error;
   1011 
   1012 	if (fhp->fid_len != sizeof(struct defid)) {
   1013 		DPRINTF(("fid_len %d %zd\n", fhp->fid_len,
   1014 		    sizeof(struct defid)));
   1015 		return EINVAL;
   1016 	}
   1017 	memcpy(&defh, fhp, sizeof(defh));
   1018 	error = msdosfs_fh_lookup(pmp, defh.defid_dirclust, defh.defid_dirofs,
   1019 	    &gen);
   1020 	if (error == 0 && gen != defh.defid_gen)
   1021 		error = ESTALE;
   1022 	if (error) {
   1023 		*vpp = NULLVP;
   1024 		return error;
   1025 	}
   1026 	error = deget(pmp, defh.defid_dirclust, defh.defid_dirofs, &dep);
   1027 	if (error) {
   1028 		DPRINTF(("deget %d\n", error));
   1029 		*vpp = NULLVP;
   1030 		return (error);
   1031 	}
   1032 	*vpp = DETOV(dep);
   1033 	return (0);
   1034 }
   1035 
   1036 int
   1037 msdosfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
   1038 {
   1039 	struct msdosfsmount *pmp = VFSTOMSDOSFS(vp->v_mount);
   1040 	struct denode *dep;
   1041 	struct defid defh;
   1042 	int error;
   1043 
   1044 	if (*fh_size < sizeof(struct defid)) {
   1045 		*fh_size = sizeof(struct defid);
   1046 		return E2BIG;
   1047 	}
   1048 	*fh_size = sizeof(struct defid);
   1049 	dep = VTODE(vp);
   1050 	memset(&defh, 0, sizeof(defh));
   1051 	defh.defid_len = sizeof(struct defid);
   1052 	defh.defid_dirclust = dep->de_dirclust;
   1053 	defh.defid_dirofs = dep->de_diroffset;
   1054 	error = msdosfs_fh_enter(pmp, dep->de_dirclust, dep->de_diroffset,
   1055 	     &defh.defid_gen);
   1056 	if (error == 0)
   1057 		memcpy(fhp, &defh, sizeof(defh));
   1058 	return error;
   1059 }
   1060 
   1061 int
   1062 msdosfs_vget(struct mount *mp, ino_t ino,
   1063     struct vnode **vpp)
   1064 {
   1065 
   1066 	return (EOPNOTSUPP);
   1067 }
   1068 
   1069 int
   1070 msdosfs_suspendctl(struct mount *mp, int cmd)
   1071 {
   1072 	int error;
   1073 	struct lwp *l = curlwp;
   1074 
   1075 	switch (cmd) {
   1076 	case SUSPEND_SUSPEND:
   1077 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
   1078 			return error;
   1079 		error = msdosfs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
   1080 		if (error == 0)
   1081 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
   1082 		if (error != 0) {
   1083 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
   1084 			return error;
   1085 		}
   1086 		return 0;
   1087 
   1088 	case SUSPEND_RESUME:
   1089 		return fstrans_setstate(mp, FSTRANS_NORMAL);
   1090 
   1091 	default:
   1092 		return EINVAL;
   1093 	}
   1094 }
   1095