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